001: /*
002: * (c) Copyright 2007 by Volker Bergmann. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, is permitted under the terms of the
006: * GNU General Public License.
007: *
008: * For redistributing this software or a derivative work under a license other
009: * than the GPL-compatible Free Software License as defined by the Free
010: * Software Foundation or approved by OSI, you must first obtain a commercial
011: * license to this software product from Volker Bergmann.
012: *
013: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
014: * WITHOUT A WARRANTY OF ANY KIND. ALL EXPRESS OR IMPLIED CONDITIONS,
015: * REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF
016: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE
017: * HEREBY EXCLUDED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
018: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
019: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
020: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
021: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
022: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
023: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
024: * POSSIBILITY OF SUCH DAMAGE.
025: */
026:
027: package org.databene.model.data;
028:
029: import org.databene.commons.Composite;
030: import org.databene.commons.OrderedMap;
031:
032: import java.util.Map;
033:
034: /**
035: * Abstraction of an entity.<br/>
036: * <br/>
037: * Created: 20.08.2007 19:20:22
038: */
039: public class Entity implements Composite {
040:
041: private OrderedMap<String, Object> components;
042: private EntityDescriptor descriptor;
043:
044: /**
045: *
046: * @param descriptor the name of the entity, it may be null
047: * @param componentKeyValues
048: */
049: public Entity(EntityDescriptor descriptor,
050: Object... componentKeyValues) {
051: this .descriptor = descriptor;
052: this .components = new OrderedMap<String, Object>();
053: for (int i = 0; i < componentKeyValues.length; i += 2)
054: setComponent((String) componentKeyValues[i],
055: componentKeyValues[i + 1]);
056: }
057:
058: public String getName() {
059: return descriptor.getName();
060: }
061:
062: public EntityDescriptor getDescriptor() {
063: return descriptor;
064: }
065:
066: /**
067: * Allows for generic 'map-like' access to component values, e.g. by FreeMarker.
068: * @param componentName the name of the component whose value to return.
069: * @return the value of the specified component.
070: * @since 0.4.0
071: */
072: public Object get(String componentName) {
073: return getComponent(componentName);
074: }
075:
076: public Object getComponent(String componentName) {
077: Object component = components.get(componentName);
078: if (component == null && !descriptor.isCaseSensitive()) {
079: Map.Entry<String, Object> entry = getComponentEntry(componentName);
080: if (entry != null)
081: component = entry.getValue();
082: }
083: return component;
084: }
085:
086: public boolean componentIsSet(String componentName) {
087: return components.containsKey(componentName);
088: }
089:
090: public Map<String, Object> getComponents() {
091: return components;
092: }
093:
094: public void setComponent(String componentName, Object component) {
095: Map.Entry<String, Object> entry = getComponentEntry(componentName);
096: if (entry != null)
097: entry.setValue(component);
098: else
099: this .components.put(componentName, component);
100: }
101:
102: // java.lang.overrides ---------------------------------------------------------------------------------------------
103:
104: public String toString() {
105: return descriptor.getName() + components;
106: }
107:
108: public boolean equals(Object o) {
109: if (this == o)
110: return true;
111: if (o == null || getClass() != o.getClass())
112: return false;
113: final Entity that = (Entity) o;
114: if (!this .descriptor.getName()
115: .equals(that.descriptor.getName()))
116: return false;
117: return this .components.equalsIgnoreOrder(that.components);
118: }
119:
120: public int hashCode() {
121: return descriptor.hashCode() * 29 + components.hashCode();
122: }
123:
124: // helper methods --------------------------------------------------------------------------------------------------
125:
126: protected Map.Entry<String, Object> getComponentEntry(
127: String componentName) {
128: for (Map.Entry<String, Object> entry : components.entrySet())
129: if (entry.getKey().equals(componentName)
130: || (!descriptor.isCaseSensitive() && entry.getKey()
131: .equalsIgnoreCase(componentName)))
132: return entry;
133: return null;
134: }
135:
136: }
|