com.dyuproject.protostuff.StringMapSchema Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of protostuff-collectionschema Show documentation
Show all versions of protostuff-collectionschema Show documentation
predefined schemas for jdk collections and maps
The newest version!
//========================================================================
//Copyright 2007-2010 David Yu [email protected]
//------------------------------------------------------------------------
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//http://www.apache.org/licenses/LICENSE-2.0
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.
//========================================================================
package com.dyuproject.protostuff;
import java.io.IOException;
import java.util.Map;
/**
* A schema for a {@link Map} with {@link String} keys.
* The key and value can be null (depending on the particular map impl).
*
* @author David Yu
* @created Jun 25, 2010
*/
public class StringMapSchema extends MapSchema
{
/**
* The schema for Map
*/
public static final StringMapSchema VALUE_STRING = new StringMapSchema(null)
{
protected void putValueFrom(Input input, MapWrapper wrapper,
String key) throws IOException
{
wrapper.put(key, input.readString());
}
protected void writeValueTo(Output output, int fieldNumber, String value,
boolean repeated) throws IOException
{
output.writeString(fieldNumber, value, repeated);
}
protected void transferValue(Pipe pipe, Input input, Output output, int number,
boolean repeated) throws IOException
{
input.transferByteRangeTo(output, true, number, repeated);
}
};
/**
* The schema of the message value.
*/
public final Schema vSchema;
/**
* The pipe schema of the message value.
*/
public final Pipe.Schema vPipeSchema;
public StringMapSchema(Schema vSchema)
{
this(vSchema, null);
}
public StringMapSchema(Schema vSchema, Pipe.Schema vPipeSchema)
{
this.vSchema = vSchema;
this.vPipeSchema = vPipeSchema;
}
protected final String readKeyFrom(Input input, MapWrapper wrapper)
throws IOException
{
return input.readString();
}
protected void putValueFrom(Input input, MapWrapper wrapper, String key)
throws IOException
{
wrapper.put(key, input.mergeObject(null, vSchema));
}
protected final void writeKeyTo(Output output, int fieldNumber, String value,
boolean repeated) throws IOException
{
output.writeString(fieldNumber, value, repeated);
}
protected void writeValueTo(Output output, int fieldNumber, V value,
boolean repeated) throws IOException
{
output.writeObject(fieldNumber, value, vSchema, repeated);
}
protected void transferKey(Pipe pipe, Input input, Output output, int number,
boolean repeated) throws IOException
{
input.transferByteRangeTo(output, true, number, repeated);
}
protected void transferValue(Pipe pipe, Input input, Output output, int number,
boolean repeated) throws IOException
{
if(vPipeSchema == null)
{
throw new RuntimeException("No pipe schema for value: " +
vSchema.typeClass().getName());
}
output.writeObject(number, pipe, vPipeSchema, repeated);
}
}