001 /*
002 * JBoss, Home of Professional Open Source.
003 * Copyright 2008, Red Hat Middleware LLC, and individual contributors
004 * as indicated by the @author tags. See the copyright.txt file in the
005 * distribution for a full listing of individual contributors.
006 *
007 * This is free software; you can redistribute it and/or modify it
008 * under the terms of the GNU Lesser General Public License as
009 * published by the Free Software Foundation; either version 2.1 of
010 * the License, or (at your option) any later version.
011 *
012 * This software is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * You should have received a copy of the GNU Lesser General Public
018 * License along with this software; if not, write to the Free
019 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021 */
022 package org.jboss.dna.connector.inmemory;
023
024 import java.util.HashMap;
025 import java.util.LinkedList;
026 import java.util.List;
027 import java.util.Map;
028 import java.util.UUID;
029 import net.jcip.annotations.NotThreadSafe;
030 import org.jboss.dna.graph.ExecutionContext;
031 import org.jboss.dna.graph.properties.Name;
032 import org.jboss.dna.graph.properties.Path;
033 import org.jboss.dna.graph.properties.Property;
034 import org.jboss.dna.graph.properties.PropertyFactory;
035
036 /**
037 * @author Randall Hauch
038 */
039 @NotThreadSafe
040 public class Node {
041
042 private final UUID uuid;
043 private Node parent;
044 private Path.Segment name;
045 private final Map<Name, Property> properties = new HashMap<Name, Property>();
046 private final List<Node> children = new LinkedList<Node>();
047
048 public Node( UUID uuid ) {
049 assert uuid != null;
050 this.uuid = uuid;
051 }
052
053 /**
054 * @return uuid
055 */
056 public UUID getUuid() {
057 return uuid;
058 }
059
060 /**
061 * @return name
062 */
063 public Path.Segment getName() {
064 return name;
065 }
066
067 /**
068 * @param name Sets name to the specified value.
069 */
070 protected void setName( Path.Segment name ) {
071 this.name = name;
072 }
073
074 /**
075 * @return parent
076 */
077 public Node getParent() {
078 return parent;
079 }
080
081 /**
082 * @param parent Sets parent to the specified value.
083 */
084 protected void setParent( Node parent ) {
085 this.parent = parent;
086 }
087
088 /**
089 * @return children
090 */
091 protected List<Node> getChildren() {
092 return children;
093 }
094
095 /**
096 * @return properties
097 */
098 protected Map<Name, Property> getProperties() {
099 return properties;
100 }
101
102 public Node setProperty( Property property ) {
103 if (property != null) {
104 this.properties.put(property.getName(), property);
105 }
106 return this;
107 }
108
109 public Node setProperty( ExecutionContext context,
110 String name,
111 Object... values ) {
112 PropertyFactory propertyFactory = context.getPropertyFactory();
113 Name propertyName = context.getValueFactories().getNameFactory().create(name);
114 return setProperty(propertyFactory.create(propertyName, values));
115 }
116
117 public Property getProperty( ExecutionContext context,
118 String name ) {
119 Name propertyName = context.getValueFactories().getNameFactory().create(name);
120 return getProperty(propertyName);
121 }
122
123 public Property getProperty( Name name ) {
124 return this.properties.get(name);
125 }
126
127 /**
128 * {@inheritDoc}
129 *
130 * @see java.lang.Object#hashCode()
131 */
132 @Override
133 public int hashCode() {
134 return uuid.hashCode();
135 }
136
137 /**
138 * {@inheritDoc}
139 *
140 * @see java.lang.Object#equals(java.lang.Object)
141 */
142 @Override
143 public boolean equals( Object obj ) {
144 if (obj == this) return true;
145 if (obj instanceof Node) {
146 Node that = (Node)obj;
147 if (!this.getUuid().equals(that.getUuid())) return false;
148 return true;
149 }
150 return false;
151 }
152
153 /**
154 * {@inheritDoc}
155 *
156 * @see java.lang.Object#toString()
157 */
158 @Override
159 public String toString() {
160 StringBuilder sb = new StringBuilder();
161 if (this.name == null) {
162 sb.append("");
163 } else {
164 sb.append(this.name);
165 }
166 sb.append(" (").append(uuid).append(")");
167 return sb.toString();
168 }
169 }