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