/*
* Copyright 2012 Robert Philipp
*
* 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 org.freezedry.persistence.builders;
import org.freezedry.persistence.PersistenceEngine;
import org.freezedry.persistence.tree.InfoNode;
public class StringNodeBuilder extends AbstractLeafNodeBuilder {
/**
* Constructs the {@link NodeBuilder} for going between primitives, their wrappers, {@link String}s and
* back to {@link Object}s.
* @param engine The {@link PersistenceEngine}
*/
public StringNodeBuilder( final PersistenceEngine engine )
{
super( engine );
}
/**
* Default no-arg constructor
*/
public StringNodeBuilder()
{
super();
}
/**
* Copy constructor
* @param builder
*/
public StringNodeBuilder( final StringNodeBuilder builder )
{
super( builder );
}
/*
* (non-Javadoc)
* @see org.freezedry.persistence.builders.AbstractLeafNodeBuilder#createInfoNode(java.lang.Object, java.lang.String)
*/
@Override
public InfoNode createInfoNode( final Object object, final String persistName )
{
// grab the class for the object to persist
final Class< ? > clazz = object.getClass();
// we must convert the object to the appropriate format
final InfoNode stringNode = InfoNode.createLeafNode( "value", (String)object, "value", String.class );
// create the root node and add the string rep of the date
final InfoNode node = InfoNode.createRootNode( persistName, clazz );
node.addChild( stringNode );
// return the node
return node;
}
/*
* (non-Javadoc)
* @see org.freezedry.persistence.builders.infonodes.NodeBuilder#createObject(java.lang.Class, org.freezedry.persistence.tree.nodes.InfoNode)
*/
@Override
public String createObject( final Class< ? > containingClass, final Class< ? > clazz, final InfoNode node )
{
final Object valueString = node.getValue();
return valueString.toString();
}
/*
* (non-Javadoc)
* @see org.freezedry.persistence.builders.AbstractLeafNodeBuilder#createObject(java.lang.Class, org.freezedry.persistence.tree.InfoNode)
*/
@Override
public String createObject( final Class< ? > clazz, final InfoNode node ) throws ReflectiveOperationException
{
final InfoNode valueNode = node.getChild( 0 );
final String value = (String)valueNode.getValue();
return value;
}
/*
* (non-Javadoc)
* @see com.synapse.copyable.Copyable#getCopy()
*/
@Override
public StringNodeBuilder getCopy()
{
return new StringNodeBuilder( this );
}
}