001: /*
002: * Enhydra Java Application Server Project
003: *
004: * The contents of this file are subject to the Enhydra Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License on
007: * the Enhydra web site ( http://www.enhydra.org/ ).
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
011: * the License for the specific terms governing rights and limitations
012: * under the License.
013: *
014: * The Initial Developer of the Enhydra Application Server is Lutris
015: * Technologies, Inc. The Enhydra Application Server and portions created
016: * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
017: * All Rights Reserved.
018: */
019: package org.enhydra.zeus.binding;
020:
021: import java.util.Iterator;
022: import java.util.LinkedList;
023: import java.util.List;
024:
025: /**
026: * <p>
027: * <code>{@link Container}</code> implements the
028: * <code>{@link org.enhydra.zeus.Binding}</code>
029: * interface and defines behavior for a binding that can contain other
030: * bindings (usually <code>{@link Property}</code> implementations). It
031: * is used to represent objects which have nested objects.
032: * </p><p>
033: * This base implementation of <code>Container</code> defines the common
034: * functionality for all <code>Container</code> implementations, removing
035: * a need for them to duplicate code for this functionality. All
036: * implementations of <code>Container</code> should extend this class
037: * rather than directly implementing the <code>Container</code> interface.
038: * </p>
039: *
040: * @author Brett McLaughlin
041: */
042: public abstract class BaseContainer extends BaseBinding implements
043: Container {
044:
045: /**
046: * The <code>{@link Property}</code> objects this
047: * <code>Container</code> holds.
048: */
049: protected List properties;
050:
051: /**
052: * <p>
053: * This will add a <code>{@link Property}</code> to
054: * the member variables of this <code>Container</code>.
055: * </p><p>
056: * It is important to note that this is simply an instance
057: * variable being added to (in most cases) a custom
058: * Java class definition. That variable/property
059: * doesn't have a value (although it might have a
060: * default value) until marshalling and unmarshalling
061: * occurs.
062: * </p>
063: *
064: * @param property <code>Property</code> to add.
065: */
066: public void addProperty(Property property) {
067: if (property == null) {
068: throw new IllegalArgumentException(
069: "A Container cannot have "
070: + "null properties as children.");
071: }
072:
073: if (properties == null) {
074: properties = new LinkedList();
075: }
076:
077: properties.add(property);
078: }
079:
080: /**
081: * <p>
082: * This will remove a <code>{@link Property}</code>
083: * from this <code>Container</code>, given the
084: * property's name. If a successful removal occurs,
085: * the boolean value <code>true</code> is retruned.
086: * If no <code>Property<code> is found with the supplied
087: * Java name, the boolean value <code>false</code> is returned.
088: * </p>
089: *
090: * @param javaName <code>String</code> Java name of
091: * <code>Property</code> to remove.
092: * @return <code>boolean</code> - indicates whether
093: * the specified named <code>Property</code>
094: * was found and removed.
095: */
096: public boolean removeProperty(String javaName) {
097: if (properties == null) {
098: return false;
099: }
100:
101: for (Iterator i = properties.iterator(); i.hasNext();) {
102: Property property = (Property) i.next();
103: if (property.getJavaName().equals(javaName)) {
104: i.remove();
105: return true;
106: }
107: }
108:
109: return false;
110: }
111:
112: /**
113: * <p>
114: * This does a wholesale replacement of this binding's current
115: * properties, removing all current ones and replacing with the
116: * supplied <code>List</code> of new properties.
117: * </p>
118: *
119: * @param properties <code>List</code> of properties to use for this
120: * container.
121: */
122: public void setProperties(List properties) {
123: if (properties == null) {
124: throw new IllegalArgumentException(
125: "A Container cannot have a " + "null property set.");
126: }
127:
128: this .properties = properties;
129: }
130:
131: /**
132: * <p>
133: * This will return a list of all the
134: * <code>{@link Property}</code> objects that
135: * this <code>Container</code> has. If there are
136: * none, this will return an empty <code>List</code>.
137: * </p>
138: *
139: * @return <code>List</code> - properties for this
140: * <code>Container</code>.
141: */
142: public List getProperties() {
143: if (properties == null) {
144: return new LinkedList();
145: }
146:
147: return properties;
148: }
149:
150: /**
151: * <p>
152: * This will clear all the properties for this <code>Container</code>.
153: * </p>
154: */
155: public void clearProperties() {
156: if (properties != null) {
157: properties.clear();
158: }
159: }
160: }
|