如何将java.lang.Appendable包装到java.io.Writer中?
更新2:我自己版本的适配器类,只调用构造函数中的如何将java.lang.Appendable包装到java.io.Writer中?,java,io,writer,Java,Io,Writer,更新2:我自己版本的适配器类,只调用构造函数中的instanceof,并在flush()和close()函数中使用(Java 1.5)增量(避免对象构造后需要任何反射或逻辑),包含在本文的底部。更新1:Marc Baumbach编写了一个简单的适配器,这正是我需要的。包括在下面。原来的问题如下 需要A的函数可以接受A,因为Writer实现了appendeable 反过来呢?我正在使用一个需要编写器的函数,我正在尝试创建另一个调用该函数的函数,该函数接受一个可追加项并将其传递给原始编写器函数 我
instanceof
,并在flush()
和close()
函数中使用(Java 1.5)增量(避免对象构造后需要任何反射或逻辑),包含在本文的底部。更新1:Marc Baumbach编写了一个简单的适配器,这正是我需要的。包括在下面。原来的问题如下
需要A的函数可以接受A,因为
Writer
实现了appendeable
反过来呢?我正在使用一个需要编写器的函数,我正在尝试创建另一个调用该函数的函数,该函数接受一个可追加项并将其传递给原始编写器函数
我发现您可以扩展抽象的Writer
,并将所有write(…)
函数重定向到它们相应的append(…)
-s。但您还必须实现和,我很清楚如何干净地编写它们,这样这个包装器类就可以接受任何可追加的内容
我感到惊讶的是,无论是在web或stackoverflow上,还是在现有的库中,都没有任何东西可以解决这个问题。至少我找不到
我希望你能给我一点指导。多谢各位
回答此问题的适配器代码。作者Marc Baumbach(我自己的版本如下):
这是我自己的版本,基于Marc的版本,它只在构造函数中使用
instanceof
,在flush()
和close()
中使用(Java 1.5)增量。这是为了避免在对象构造之后必须使用任何逻辑或反射。本文件还发布为:
这个类包含一个demo,后面是两个不做任何事情的delta(一个Flushable
,一个Closeable
),主函数(newwriterforappendeble(apbl)
),然后是适配器类本身
import java.io.Closeable;
import java.io.Flushable;
import java.io.IOException;
import java.io.Writer;
/**
<P>{@code java NewWriterForAppendable}.</P>
**/
public class NewWriterForAppendable {
/**
<P>Demonstrates {@code newWriterForAppendable(apbl)} for creating a new {@code Writer} that wraps around {@code System.out} (writes to the console).</P>
**/
public static final void main(String[] igno_red) {
try {
NewWriterForAppendable.newWriterForAppendable(System.out).write("hello");
} catch(IOException iox) {
throw new RuntimeException("WriterForAppendableXmpl", iox);
}
}
/**
<P>A {@code Flushable} whose {@code flush()} function does nothing. This is used by {@link #newWriterForAppendable(Appendable ap_bl) newWriterForAppendable}{@code (apbl)} as a (Java 1.5) delta.</P>
@see #newWriterForAppendable(Appendable) newWriterForAppendable(apbl)
**/
public static final Flushable FLUSHABLE_DO_NOTHING = new Flushable() {
public void flush() {
}
};
/**
<P>A {@code Closeable} whose {@code close()} function does nothing. This is used by {@link #newWriterForAppendable(Appendable ap_bl) newWriterForAppendable}{@code (apbl)} as a (Java 1.5) delta.</P>
@see #newWriterForAppendable(Appendable) newWriterForAppendable(apbl)
**/
public static final Closeable CLOSEABLE_DO_NOTHING = new Closeable() {
public void close() {
}
};
/**
<P>Creates a new {@code java.io.Writer} that wraps around a {@code java.lang.Appendable}. It properly {@link java.io.Writer#flush() flush}es and {@link java.io.Writer#close() close}s appendables that happened to also be {@link java.io.Flushable}s and/or {@link java.io.Closeable Closeable}s. This uses {@code instanceof} only in the constructor, and a delta in {@code flush()} and {@code close()}, which avoids having to use any logic or reflection after object construction.</P>
<P>This function is released as a <A HREF="https://gist.github.com/aliteralmind/8494917">gist</A>, and is an example of the <A HREF="http://en.wikipedia.org/wiki/Adapter_pattern#Object_Adapter_pattern">Object Adapter pattern</A>. Thanks to <A HREF="http://stackoverflow.com/users/1211906/marc-baumbach">Marc Baumbach</A> on <A HREF="http://stackoverflow.com">{@code stackoverflow}</A> for the assistance. See (viewed 1/18/2014)
<BR> <CODE><A HREF="http://stackoverflow.com/questions/21200421/how-to-wrap-a-java-lang-appendable-into-a-java-io-writer">http://stackoverflow.com/questions/21200421/how-to-wrap-a-java-lang-appendable-into-a-java-io-writer</A></CODE></P>
@return A new writer that uses an appendable to do its output.
@see #FLUSHABLE_DO_NOTHING
@see #CLOSEABLE_DO_NOTHING
**/
public static final Writer newWriterForAppendable(Appendable ap_bl) {
return (new WFA(ap_bl));
}
private NewWriterForAppendable() {
throw new IllegalStateException("constructor: Do not instantiate.");
}
}
class WFA extends Writer {
private final Appendable apbl;
private final Flushable flbl;
private final Closeable clbl;
public WFA(Appendable ap_bl) {
if(ap_bl == null) {
throw new NullPointerException("ap_bl");
}
apbl = ap_bl;
//Avoids instanceof at every call to flush() and close()
flbl = (Flushable)((ap_bl instanceof Flushable) ? ap_bl
: NewWriterForAppendable.FLUSHABLE_DO_NOTHING);
clbl = (Closeable)((ap_bl instanceof Closeable) ? ap_bl
: NewWriterForAppendable.CLOSEABLE_DO_NOTHING);
}
@Override
public void write(char[] a_c, int i_ndexStart, int i_ndexEndX) throws IOException {
apbl.append(String.valueOf(a_c), i_ndexStart, i_ndexEndX);
}
@Override
public Writer append(char c_c) throws IOException {
apbl.append(c_c);
return this;
}
@Override
public Writer append(CharSequence c_q) throws IOException {
apbl.append(c_q);
return this;
}
@Override
public Writer append(CharSequence c_q, int i_ndexStart, int i_ndexEndX) throws IOException {
apbl.append(c_q, i_ndexStart, i_ndexEndX);
return this;
}
@Override
public void flush() throws IOException {
flbl.flush();
}
@Override
public void close() throws IOException {
flush();
clbl.close();
}
}
import java.io.Closeable;
导入java.io.Flushable;
导入java.io.IOException;
导入java.io.Writer;
/**
{@code java newwriter forappendeble}。
**/
公共类newWriterForAppendeable{
/**
演示{@code-newwriter-forappendeable(apbl)},用于创建一个新的{@code-Writer},它围绕{@code-System.out}(写入控制台)。
**/
公共静态最终空干管(字符串[]忽略红色){
试一试{
newwriterforappendeble.newwriterforappendeble(System.out).write(“hello”);
}捕获(iox异常){
抛出新的运行时异常(“writerForAppendebleXmpl”,iox);
}
}
/**
一种{@code Flushable},其{@code flush()}函数不执行任何操作。它被{@link#newWriterForAppendable(appendeable ap#bl)newWriterForAppendable}{@code(apbl)}用作(Java 1.5)增量。
@参见#newwriter可追加(appendeable)newwriter可追加(apbl)
**/
public static final Flushable Flushable_DO_NOTHING=新Flushable(){
公共图书馆{
}
};
/**
一种{@code Closeable},其{@code close()}函数不执行任何操作。它被{@link#newwriterforappendeable(appendeable ap#bl)newwriterforappendeable}{@code(apbl)}用作(Java 1.5)增量。
@参见#newwriter可追加(appendeable)newwriter可追加(apbl)
**/
public static final Closeable Closeable\u DO\u NOTHING=new Closeable(){
公众假期结束(){
}
};
/**
创建一个新的{@code java.io.Writer},它将{@code java.lang.Appendable}包装起来。它正确地{@link java.io.Writer#flush()flush}es和{@link java.io.Writer#close()close}的Appendable恰好也是{@link java.io.Flushable}和/或{@link java.io.Closeable Closeable Closeable}。它使用{@code instanceof}仅在构造函数中,以及{@code flush()}和{@code close()}中的增量,这避免了在对象构造后必须使用任何逻辑或反射。
此功能作为发布,是的一个示例。感谢on的帮助。请参阅(查看1/18/2014)
@返回一个新的编写器,该编写器使用appendable进行输出。
@看#可冲洗#你什么都不做#
@见#可关闭#你什么也不做#
**/
公共静态最终编写器newwriter可追加(可追加的ap\U bl){
申报表(新WFA(ap_bl));
}
私有newWriterForAppendeable(){
抛出新的IllegalStateException(“构造函数:不实例化”);
}
}
类WFA扩展了Writer{
私有最终可追加apbl;
私人最终可冲洗flbl;
私人最终可关闭clbl;
公共WFA(可追加ap_bl){
如果(ap_bl==null){
抛出新的NullPointerException(“ap_bl”);
}
apbl=ap_bl;
//在每次调用flush()和close()时避免instanceof
flbl=(可冲洗)((可冲洗的ap\U bl实例)?ap\U bl
:newwriter for appended.FLUSHABLE\u DO\u NOTHING);
clbl=(可关闭)((可关闭的ap\U bl实例)?ap\U bl
:newWriterforappended.CLOSEABLE\u DO\u NOTHING);
}
@凌驾
公共无效写入(char[]a\u c,int i\u ndexStart,int i\u ndexEndX)引发IOException{
apbl.append(String.valueOf(a_c)、i_ndexStart、i_ndexEndX);
}
@凌驾
公共写入程序附加(char c_c)引发IOException{
apbl.append(c_c);
归还这个;
}
@凌驾
公共写入程序附加(CharSequence c_q)引发IOException{
apbl.append(c_q);
归还这个;
}
@凌驾
公共编写器附加(CharSequence c_q、int i_ndexStart、int i_ndexEndX)抛出IOException{
apbl.append(c_q,i_ndexStart,i_ndexEndX);
归还这个;
}
@凌驾
public void flush()引发IOException{
flbl.flush();
}
@凌驾
public void close()引发IOException{
冲洗();
clbl.close();
}
}
您可以接受任何可追加的
,然后通过指令检查它是否是编写器
import java.io.Closeable;
import java.io.Flushable;
import java.io.IOException;
import java.io.Writer;
/**
<P>{@code java NewWriterForAppendable}.</P>
**/
public class NewWriterForAppendable {
/**
<P>Demonstrates {@code newWriterForAppendable(apbl)} for creating a new {@code Writer} that wraps around {@code System.out} (writes to the console).</P>
**/
public static final void main(String[] igno_red) {
try {
NewWriterForAppendable.newWriterForAppendable(System.out).write("hello");
} catch(IOException iox) {
throw new RuntimeException("WriterForAppendableXmpl", iox);
}
}
/**
<P>A {@code Flushable} whose {@code flush()} function does nothing. This is used by {@link #newWriterForAppendable(Appendable ap_bl) newWriterForAppendable}{@code (apbl)} as a (Java 1.5) delta.</P>
@see #newWriterForAppendable(Appendable) newWriterForAppendable(apbl)
**/
public static final Flushable FLUSHABLE_DO_NOTHING = new Flushable() {
public void flush() {
}
};
/**
<P>A {@code Closeable} whose {@code close()} function does nothing. This is used by {@link #newWriterForAppendable(Appendable ap_bl) newWriterForAppendable}{@code (apbl)} as a (Java 1.5) delta.</P>
@see #newWriterForAppendable(Appendable) newWriterForAppendable(apbl)
**/
public static final Closeable CLOSEABLE_DO_NOTHING = new Closeable() {
public void close() {
}
};
/**
<P>Creates a new {@code java.io.Writer} that wraps around a {@code java.lang.Appendable}. It properly {@link java.io.Writer#flush() flush}es and {@link java.io.Writer#close() close}s appendables that happened to also be {@link java.io.Flushable}s and/or {@link java.io.Closeable Closeable}s. This uses {@code instanceof} only in the constructor, and a delta in {@code flush()} and {@code close()}, which avoids having to use any logic or reflection after object construction.</P>
<P>This function is released as a <A HREF="https://gist.github.com/aliteralmind/8494917">gist</A>, and is an example of the <A HREF="http://en.wikipedia.org/wiki/Adapter_pattern#Object_Adapter_pattern">Object Adapter pattern</A>. Thanks to <A HREF="http://stackoverflow.com/users/1211906/marc-baumbach">Marc Baumbach</A> on <A HREF="http://stackoverflow.com">{@code stackoverflow}</A> for the assistance. See (viewed 1/18/2014)
<BR> <CODE><A HREF="http://stackoverflow.com/questions/21200421/how-to-wrap-a-java-lang-appendable-into-a-java-io-writer">http://stackoverflow.com/questions/21200421/how-to-wrap-a-java-lang-appendable-into-a-java-io-writer</A></CODE></P>
@return A new writer that uses an appendable to do its output.
@see #FLUSHABLE_DO_NOTHING
@see #CLOSEABLE_DO_NOTHING
**/
public static final Writer newWriterForAppendable(Appendable ap_bl) {
return (new WFA(ap_bl));
}
private NewWriterForAppendable() {
throw new IllegalStateException("constructor: Do not instantiate.");
}
}
class WFA extends Writer {
private final Appendable apbl;
private final Flushable flbl;
private final Closeable clbl;
public WFA(Appendable ap_bl) {
if(ap_bl == null) {
throw new NullPointerException("ap_bl");
}
apbl = ap_bl;
//Avoids instanceof at every call to flush() and close()
flbl = (Flushable)((ap_bl instanceof Flushable) ? ap_bl
: NewWriterForAppendable.FLUSHABLE_DO_NOTHING);
clbl = (Closeable)((ap_bl instanceof Closeable) ? ap_bl
: NewWriterForAppendable.CLOSEABLE_DO_NOTHING);
}
@Override
public void write(char[] a_c, int i_ndexStart, int i_ndexEndX) throws IOException {
apbl.append(String.valueOf(a_c), i_ndexStart, i_ndexEndX);
}
@Override
public Writer append(char c_c) throws IOException {
apbl.append(c_c);
return this;
}
@Override
public Writer append(CharSequence c_q) throws IOException {
apbl.append(c_q);
return this;
}
@Override
public Writer append(CharSequence c_q, int i_ndexStart, int i_ndexEndX) throws IOException {
apbl.append(c_q, i_ndexStart, i_ndexEndX);
return this;
}
@Override
public void flush() throws IOException {
flbl.flush();
}
@Override
public void close() throws IOException {
flush();
clbl.close();
}
}
public void myMethod(Appendable app) throws InvalidAppendableException {
if (app instanceof Writer) {
someObj.thatMethod((Writer) app);
} else {
throw new InvalidAppendableException();
}
}