-
Notifications
You must be signed in to change notification settings - Fork 236
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support copy/paste with style, via Codec<Style>.
Resolves #17.
- Loading branch information
1 parent
7cbcd72
commit fe26405
Showing
7 changed files
with
263 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package org.fxmisc.richtext; | ||
|
||
import java.io.DataInputStream; | ||
import java.io.DataOutputStream; | ||
import java.io.IOException; | ||
import java.nio.charset.Charset; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
public interface Codec<T> { | ||
|
||
String getName(); | ||
void encode(DataOutputStream os, T t) throws IOException; | ||
T decode(DataInputStream is) throws IOException; | ||
|
||
|
||
static final Codec<String> STRING_CODEC = new Codec<String>() { | ||
private final Charset UTF8 = Charset.forName("UTF-8"); | ||
|
||
@Override | ||
public String getName() { | ||
return "string"; | ||
} | ||
|
||
@Override | ||
public void encode(DataOutputStream os, String s) throws IOException { | ||
os.writeUTF(s); | ||
} | ||
|
||
@Override | ||
public String decode(DataInputStream is) throws IOException { | ||
return is.readUTF(); | ||
} | ||
}; | ||
|
||
static <T> Codec<List<T>> listCodec(Codec<T> elemCodec) { | ||
return SuperCodec.collectionListCodec(elemCodec); | ||
} | ||
} | ||
|
||
interface SuperCodec<S, T extends S> extends Codec<T> { | ||
void encodeSuper(DataOutputStream os, S s) throws IOException; | ||
|
||
@Override | ||
default void encode(DataOutputStream os, T t) throws IOException { | ||
encodeSuper(os, t); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
static <S, U extends S, T extends U> SuperCodec<S, U> upCast(SuperCodec<S, T> sc) { | ||
return (SuperCodec<S, U>) sc; | ||
} | ||
|
||
static <T> SuperCodec<Collection<T>, List<T>> collectionListCodec(Codec<T> elemCodec) { | ||
return new SuperCodec<Collection<T>, List<T>>() { | ||
|
||
@Override | ||
public String getName() { | ||
return "list<" + elemCodec.getName() + ">"; | ||
} | ||
|
||
@Override | ||
public void encodeSuper(DataOutputStream os, Collection<T> col) throws IOException { | ||
os.writeInt(col.size()); | ||
for(T t: col) { | ||
elemCodec.encode(os, t); | ||
} | ||
} | ||
|
||
@Override | ||
public List<T> decode(DataInputStream is) throws IOException { | ||
int size = is.readInt(); | ||
List<T> elems = new ArrayList<>(size); | ||
for(int i = 0; i < size; ++i) { | ||
T t = elemCodec.decode(is); | ||
elems.add(t); | ||
} | ||
return elems; | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters