📄 raplamapimpl.java
字号:
/*--------------------------------------------------------------------------*
| Copyright (C) 2006 Christopher Kohlhaas |
| |
| This program is free software; you can redistribute it and/or modify |
| it under the terms of the GNU General Public License as published by the |
| Free Software Foundation. A copy of the license has been included with |
| these distribution in the COPYING file, if not go to www.fsf.org |
| |
| As a special exception, you are granted the permissions to link this |
| program with every library, which license fulfills the Open Source |
| Definition as published by the Open Source Initiative (OSI). |
*--------------------------------------------------------------------------*/
package org.rapla.entities.configuration.internal;
import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import org.rapla.components.util.Assert;
import org.rapla.components.util.iterator.FilterIterator;
import org.rapla.components.util.iterator.IteratorChain;
import org.rapla.components.util.iterator.NestedIterator;
import org.rapla.entities.EntityNotFoundException;
import org.rapla.entities.RaplaObject;
import org.rapla.entities.RaplaType;
import org.rapla.entities.ReadOnlyException;
import org.rapla.entities.configuration.RaplaMap;
import org.rapla.entities.dynamictype.DynamicType;
import org.rapla.entities.storage.DynamicTypeDependant;
import org.rapla.entities.storage.RefEntity;
import org.rapla.entities.storage.EntityReferencer;
import org.rapla.entities.storage.EntityResolver;
import org.rapla.entities.storage.internal.ReferenceHandler;
/**
*
* @author ckohlhaas
* @version 1.00.00
* @since 2.03.00
*/
public class RaplaMapImpl implements RaplaMap,RaplaObject, Serializable,EntityReferencer, DynamicTypeDependant {
// Don't forget to increase the serialVersionUID when you change the fields
private static final long serialVersionUID = 1;
private Map map;
private ReferenceHandler referenceHandler = new ReferenceHandler();
private Map childMap = new HashMap();
/** use this constructor only for reference */
public static RaplaMap EMPTY_MAP = new RaplaMapImpl();
public RaplaMapImpl() {
this.map = new TreeMap();
}
public RaplaMapImpl( Collection list) {
this( makeMap(list) );
}
private static Map makeMap(Collection list) {
Map map = new TreeMap();
int key = 0;
for ( Iterator it = list.iterator();it.hasNext();) {
map.put( new String( String.valueOf(key++)), it.next());
}
return map;
}
public RaplaMapImpl( Map map) {
this.map = Collections.unmodifiableMap(map);
for ( Iterator it = this.map.keySet().iterator();it.hasNext();) {
String key = (String)it.next();
Object o = this.map.get(key );
if ( ! (o instanceof RaplaObject ) && !(o instanceof String) )
{
throw new IllegalArgumentException("Only map entries of type RaplaObject are allowed.");
}
if ( o instanceof RefEntity) {
getReferenceHandler().put( key, o);
} else {
childMap.put( key, o );
}
}
}
public Map getChildMap() {
return childMap;
}
/** This method is only used in storage operations, please dont use it from outside*/
public void putString(String key, String value)
{
childMap.put( key, value);
}
public Iterator getReferences() {
Iterator refIt = new NestedIterator( getEntityReferencers()) {
public Iterator getNestedIterator(Object obj) {
return ((EntityReferencer) obj).getReferences();
}
};
return new IteratorChain( refIt, getReferenceHandler().getReferences());
}
private Iterator getEntityReferencers() {
return new FilterIterator( map.values().iterator()) {
protected boolean isInIterator(Object obj) {
return obj instanceof EntityReferencer;
}
};
}
public boolean isRefering(RefEntity object) {
if ( getReferenceHandler().isRefering( object )) {
return true;
}
for (Iterator it = getEntityReferencers();it.hasNext();) {
if (((EntityReferencer) it.next()).isRefering( object)) {
return true;
}
}
return false;
}
/*
public Iterator getReferences() {
return getReferenceHandler().getReferences();
}
public boolean isRefering(Entity entity) {
return getReferenceHandler().isRefering( entity);
}*/
public void resolveEntities( EntityResolver resolver) throws EntityNotFoundException {
referenceHandler.resolveEntities( resolver );
Map map = new HashMap();
for ( Iterator it = childMap.keySet().iterator();it.hasNext();) {
String key = (String)it.next();
Object entity = childMap.get(key) ;
if ( entity instanceof EntityReferencer) {
((EntityReferencer) entity).resolveEntities( resolver);
}
map.put( key, entity);
}
for ( Iterator it = getReferenceHandler().getReferenceKeys();it.hasNext();) {
String key = (String)it.next();
RefEntity entity = (RefEntity)getReferenceHandler().get(key) ;
Assert.notNull( entity );
map.put( key, entity);
}
this.map = Collections.unmodifiableMap( map );
}
public ReferenceHandler getReferenceHandler() {
return referenceHandler;
}
public RaplaType getRaplaType() {
return TYPE;
}
public boolean needsChange(DynamicType type) {
for (Iterator it = childMap.values().iterator();it.hasNext();) {
Object obj = it.next();
if ( obj instanceof DynamicTypeDependant) {
if (((DynamicTypeDependant) obj).needsChange( type ))
return true;
}
}
return false;
}
public void commitChange(DynamicType type) {
for (Iterator it = childMap.values().iterator();it.hasNext();) {
Object obj = it.next();
if ( obj instanceof DynamicTypeDependant) {
((DynamicTypeDependant) obj).commitChange( type );
}
}
}
/**
* @see java.util.Map#size()
*/
public int size() {
return map.size();
}
/**
* @see java.util.Map#isEmpty()
*/
public boolean isEmpty() {
return map.isEmpty();
}
/**
* @see java.util.Map#containsKey(java.lang.Object)
*/
public boolean containsKey(Object key) {
return map.containsKey( key);
}
/**
* @see java.util.Map#containsValue(java.lang.Object)
*/
public boolean containsValue(Object key) {
return map.containsValue( key);
}
/**
* @see java.util.Map#get(java.lang.Object)
*/
public Object get(Object key) {
return map.get(key);
}
/**
* @see java.util.Map#put(java.lang.Object, java.lang.Object)
*/
public Object put(Object arg0, Object arg1) {
throw createReadOnlyException();
}
/**
* @see java.util.Map#remove(java.lang.Object)
*/
public Object remove(Object arg0) {
throw createReadOnlyException();
}
/**
* @see java.util.Map#putAll(java.util.Map)
*/
public void putAll(Map arg0) {
throw createReadOnlyException();
}
/**
* @see java.util.Map#clear()
*/
public void clear() {
throw createReadOnlyException();
}
ReadOnlyException createReadOnlyException() {
return new ReadOnlyException("RaplaMap is readonly you must create a new Object");
}
/**
* @see java.util.Map#keySet()
*/
public Set keySet() {
return map.keySet();
}
/**
* @see java.util.Map#values()
*/
public Collection values() {
return map.values();
}
/**
* @see java.util.Map#entrySet()
*/
public Set entrySet() {
return map.entrySet();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -