/**********************************************************************
Copyright (c) 2007 Thomas Marti and others. All rights reserved.
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.
Contributors:
...
**********************************************************************/
package org.datanucleus.store.mapped.mapping;
import java.awt.Rectangle;
import org.datanucleus.ClassLoaderResolver;
import org.datanucleus.ClassNameConstants;
import org.datanucleus.OMFContext;
import org.datanucleus.metadata.AbstractMemberMetaData;
import org.datanucleus.store.ExecutionContext;
import org.datanucleus.store.mapped.DatastoreContainerObject;
import org.datanucleus.store.mapped.MappedStoreManager;
/**
* Mapping for java.awt.Rectangle, maps the x, y, width and height values to int-precision
* datastore fields
*/
public class RectangleMapping extends SingleFieldMultiMapping
{
/* (non-Javadoc)
* @see org.datanucleus.store.mapping.JavaTypeMapping#initialize(AbstractMemberMetaData, DatastoreContainerObject, ClassLoaderResolver)
*/
public void initialize(AbstractMemberMetaData fmd, DatastoreContainerObject container, ClassLoaderResolver clr)
{
super.initialize(fmd, container, clr);
addDatastoreFields();
}
/* (non-Javadoc)
* @see org.datanucleus.store.mapped.mapping.JavaTypeMapping#initialize(MappedStoreManager, java.lang.String)
*/
public void initialize(MappedStoreManager storeMgr, String type)
{
super.initialize(storeMgr, type);
addDatastoreFields();
}
protected void addDatastoreFields()
{
addDatastoreField(ClassNameConstants.INT); // X
addDatastoreField(ClassNameConstants.INT); // Y
addDatastoreField(ClassNameConstants.INT); // Width
addDatastoreField(ClassNameConstants.INT); // Height
}
/* (non-Javadoc)
* @see org.datanucleus.store.mapping.JavaTypeMapping#getJavaType()
*/
public Class getJavaType()
{
return Rectangle.class;
}
/**
* Method to return the value to be stored in the specified datastore index given the overall
* value for this java type.
* @param index The datastore index
* @param value The overall value for this java type
* @return The value for this datastore index
*/
public Object getValueForDatastoreMapping(OMFContext omfCtx, int index, Object value)
{
Rectangle.Double rect = (Rectangle.Double)value;
if (index == 0)
{
return rect.getX();
}
else if (index == 1)
{
return rect.getY();
}
else if (index == 2)
{
return rect.getWidth();
}
else if (index == 3)
{
return rect.getHeight();
}
throw new IndexOutOfBoundsException();
}
/* (non-Javadoc)
* @see org.datanucleus.store.mapping.JavaTypeMapping#setObject(org.datanucleus.ObjectManager, java.lang.Object, int[], java.lang.Object)
*/
public void setObject(ExecutionContext ec, Object preparedStatement, int[] exprIndex, Object value)
{
Rectangle rectangle = (Rectangle)value;
if (rectangle == null)
{
for (int i = 0; i < exprIndex.length; i++)
{
getDatastoreMapping(i).setObject(preparedStatement, exprIndex[i], null);
}
}
else
{
getDatastoreMapping(0).setInt(preparedStatement,exprIndex[0],rectangle.x);
getDatastoreMapping(1).setInt(preparedStatement,exprIndex[1],rectangle.y);
getDatastoreMapping(2).setInt(preparedStatement,exprIndex[2],rectangle.width);
getDatastoreMapping(3).setInt(preparedStatement,exprIndex[3],rectangle.height);
}
}
/* (non-Javadoc)
* @see org.datanucleus.store.mapping.JavaTypeMapping#getObject(org.datanucleus.ObjectManager, java.lang.Object, int[])
*/
public Object getObject(ExecutionContext ec, Object resultSet, int[] exprIndex)
{
// Check for null entries
if (getDatastoreMapping(0).getObject(resultSet, exprIndex[0]) == null)
{
return null;
}
int x = getDatastoreMapping(0).getInt(resultSet,exprIndex[0]);
int y = getDatastoreMapping(1).getInt(resultSet,exprIndex[1]);
int width = getDatastoreMapping(2).getInt(resultSet,exprIndex[2]);
int height = getDatastoreMapping(3).getInt(resultSet,exprIndex[3]);
return new Rectangle(x, y, width, height);
}
// TODO Delete when we drop legacy support
private static final Rectangle sampleValue = new Rectangle(0, 0, 1, 1);
public Object getSampleValue(ClassLoaderResolver clr)
{
return sampleValue;
}
}
|