com.threerings.nexus.io.Streamers Maven / Gradle / Ivy
The newest version!
//
// Nexus Core - a framework for developing distributed applications
// http://github.com/threerings/nexus/blob/master/LICENSE
package com.threerings.nexus.io;
import java.util.AbstractCollection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Defines streamers for all of the basic types supported by Nexus.
*/
public class Streamers
{
/** Creates a streamer for the supplied enum class. */
public static > Streamer_Enum create (Class eclass) {
return new Streamer_Enum(eclass);
}
/** A streamer for null instances. */
public static class Streamer_Null implements Streamer {
public Class> getObjectClass () {
return Void.class;
}
public void writeObject (Streamable.Output out, Void value) {
// nothing need be written
}
public Void readObject (Streamable.Input in) {
return null;
}
}
/** A streamer for {@link Boolean}. */
public static class Streamer_Boolean implements Streamer {
public Class> getObjectClass () {
return Boolean.class;
}
public void writeObject (Streamable.Output out, Boolean value) {
out.writeBoolean(value);
}
public Boolean readObject (Streamable.Input in) {
return in.readBoolean();
}
}
/** A streamer for {@link Byte}. */
public static class Streamer_Byte implements Streamer {
public Class> getObjectClass () {
return Byte.class;
}
public void writeObject (Streamable.Output out, Byte value) {
out.writeByte(value);
}
public Byte readObject (Streamable.Input in) {
return in.readByte();
}
}
/** A streamer for {@link Character}. */
public static class Streamer_Character implements Streamer {
public Class> getObjectClass () {
return Character.class;
}
public void writeObject (Streamable.Output out, Character value) {
out.writeChar(value);
}
public Character readObject (Streamable.Input in) {
return in.readChar();
}
}
/** A streamer for {@link Short}. */
public static class Streamer_Short implements Streamer {
public Class> getObjectClass () {
return Short.class;
}
public void writeObject (Streamable.Output out, Short value) {
out.writeShort(value);
}
public Short readObject (Streamable.Input in) {
return in.readShort();
}
}
/** A streamer for {@link Integer}. */
public static class Streamer_Integer implements Streamer {
public Class> getObjectClass () {
return Integer.class;
}
public void writeObject (Streamable.Output out, Integer value) {
out.writeInt(value);
}
public Integer readObject (Streamable.Input in) {
return in.readInt();
}
}
/** A streamer for {@link Long}. */
public static class Streamer_Long implements Streamer {
public Class> getObjectClass () {
return Long.class;
}
public void writeObject (Streamable.Output out, Long value) {
out.writeLong(value);
}
public Long readObject (Streamable.Input in) {
return in.readLong();
}
}
/** A streamer for {@link Float}. */
public static class Streamer_Float implements Streamer {
public Class> getObjectClass () {
return Float.class;
}
public void writeObject (Streamable.Output out, Float value) {
out.writeFloat(value);
}
public Float readObject (Streamable.Input in) {
return in.readFloat();
}
}
/** A streamer for {@link Double}. */
public static class Streamer_Double implements Streamer {
public Class> getObjectClass () {
return Double.class;
}
public void writeObject (Streamable.Output out, Double value) {
out.writeDouble(value);
}
public Double readObject (Streamable.Input in) {
return in.readDouble();
}
}
// TODO: boxed streamers
/** A streamer for {@link String}. */
public static class Streamer_String implements Streamer {
public Class> getObjectClass () {
return String.class;
}
public void writeObject (Streamable.Output out, String value) {
out.writeString(value);
}
public String readObject (Streamable.Input in) {
return in.readString();
}
}
/** A streamer for {@link List}. */
public static class Streamer_List implements Streamer> {
public Class> getObjectClass () {
return ArrayList.class; // special handling catches other List impls
}
public void writeObject (Streamable.Output out, List values) {
writeSequence(out, values);
}
public List readObject (Streamable.Input in) {
List list = new ArrayList();
readSequence(in, list);
return list;
}
}
/** A streamer for {@link Set}. */
public static class Streamer_Set implements Streamer> {
public Class> getObjectClass () {
return HashSet.class; // special handling catches other Set impls
}
public void writeObject (Streamable.Output out, Set values) {
writeSequence(out, values);
}
public Set readObject (Streamable.Input in) {
Set set = new HashSet();
readSequence(in, set);
return set;
}
}
/** A streamer for {@link Map}. */
public static class Streamer_Map implements Streamer