org.datanucleus.metadata.ContainerMetaData Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of datanucleus-core Show documentation
Show all versions of datanucleus-core Show documentation
DataNucleus Core provides the primary components of a heterogenous Java persistence solution.
It supports persistence API's being layered on top of the core functionality.
/**********************************************************************
Copyright (c) 2004 Andy Jefferson 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.metadata;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
/**
* Representation of the Meta-Data for a container.
* This is subclassed by Array, Collection, and Map.
*/
public class ContainerMetaData extends MetaData
{
private static final long serialVersionUID = -8318504420004336339L;
/** Whether this container allows nulls to be stored (as elements/keys/values). */
Boolean allowNulls = null;
/**
* Constructor.
*/
public ContainerMetaData()
{
}
/**
* Copy constructor.
* @param contmd Container metadata to copy
*/
public ContainerMetaData(ContainerMetaData contmd)
{
super(null, contmd);
}
/**
* Method to populate any defaults, and check the validity of the MetaData.
*/
public void populate()
{
// Set the default for allowNulls if not set, based on the java type
if (parent.hasExtension(MetaData.EXTENSION_MEMBER_CONTAINER_ALLOW_NULLS))
{
allowNulls = Boolean.valueOf(parent.getValueForExtension(MetaData.EXTENSION_MEMBER_CONTAINER_ALLOW_NULLS));
}
if (allowNulls == null)
{
// Set default based on the container type
Class type = ((AbstractMemberMetaData)parent).getType();
if (type.isArray())
{
if (type.getComponentType().isPrimitive())
{
allowNulls = Boolean.FALSE;
}
else
{
allowNulls = Boolean.TRUE;
}
}
else if (type == HashMap.class)
{
allowNulls = Boolean.TRUE;
}
else if (type == Hashtable.class)
{
allowNulls = Boolean.FALSE;
}
else if (type == HashSet.class)
{
allowNulls = Boolean.TRUE;
}
else if (type == LinkedHashSet.class)
{
allowNulls = Boolean.TRUE;
}
else if (type == LinkedHashMap.class)
{
allowNulls = Boolean.TRUE;
}
else if (List.class.isAssignableFrom(type))
{
allowNulls = Boolean.TRUE;
}
// TODO Extend this for other container types
}
}
/**
* Whether this container allows nulls.
* For a collection/array this is whether there can be null elements.
* For a map this whether there can be null keys AND values (really we ought to treat them independent, but
* not done like that currently).
* @return Whether nulls are allowed to be stored in the container
*/
public Boolean allowNulls()
{
return allowNulls;
}
/**
* Accessor for the parent field/property MetaData.
* @return Parent metadata
*/
public AbstractMemberMetaData getMemberMetaData()
{
if (parent != null)
{
return (AbstractMemberMetaData)parent;
}
return null;
}
/**
* Accessor for the parent member name
* @return Parent member name.
*/
public String getMemberName()
{
if (parent != null)
{
return ((AbstractMemberMetaData)parent).getName();
}
return null;
}
}