View Javadoc

1   /*
2    * ModeShape (http://www.modeshape.org)
3    * See the COPYRIGHT.txt file distributed with this work for information
4    * regarding copyright ownership.  Some portions may be licensed
5    * to Red Hat, Inc. under one or more contributor license agreements.
6    * See the AUTHORS.txt file in the distribution for a full listing of 
7    * individual contributors. 
8    *
9    * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10   * is licensed to you under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation; either version 2.1 of
12   * the License, or (at your option) any later version.
13   *
14   * ModeShape is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this software; if not, write to the Free
21   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23   */
24  package org.modeshape.graph;
25  
26  import java.util.ArrayList;
27  import java.util.Collections;
28  import java.util.List;
29  import java.util.UUID;
30  import net.jcip.annotations.Immutable;
31  import org.modeshape.common.util.CheckArg;
32  import org.modeshape.graph.property.Name;
33  import org.modeshape.graph.property.Path;
34  import org.modeshape.graph.property.Property;
35  import org.modeshape.graph.property.basic.BasicSingleValueProperty;
36  
37  /**
38   * General purpose location type that supports a path and a property. This class should never be directly instantiated by users of
39   * the ModeShape framework. Instead, use @{link Location#create} to create the correct location.
40   * 
41   * @see Location
42   */
43  @Immutable
44  final class LocationWithProperty extends LocationWithProperties {
45  
46      private static final long serialVersionUID = 1L;
47  
48      /**
49       * Create a new location with a given path and identification property.
50       * 
51       * @param idProperty the identification property
52       */
53      LocationWithProperty( Property idProperty ) {
54          super(Collections.singletonList(idProperty));
55          assert idProperty != null;
56          assert !idProperty.isEmpty();
57      }
58  
59      /**
60       * {@inheritDoc}
61       * 
62       * @see Location#getIdProperty(Name)
63       */
64      @Override
65      public final Property getIdProperty( Name name ) {
66          CheckArg.isNotNull(name, "name");
67          Property property = getIdProperties().get(0); // this is fast
68          return property.getName().equals(name) ? property : null;
69      }
70  
71      /**
72       * {@inheritDoc}
73       * 
74       * @see Location#getUuid()
75       */
76      @Override
77      public UUID getUuid() {
78          Property property = getIdProperties().get(0); // this is fast
79          if (ModeShapeLexicon.UUID.equals(property.getName())) {
80              Object value = property.getFirstValue();
81              if (value instanceof UUID) return (UUID)value;
82              if (value instanceof String) return UUID.fromString((String)value);
83          }
84          return null;
85      }
86  
87      /**
88       * {@inheritDoc}
89       * 
90       * @see Location#with(Property)
91       */
92      @Override
93      public Location with( Property newIdProperty ) {
94          if (newIdProperty == null || newIdProperty.isEmpty()) return this;
95          Property idProperty = getIdProperties().get(0); // fast
96          if (newIdProperty.getName().equals(idProperty.getName())) {
97              return Location.create(newIdProperty);
98          }
99          List<Property> newIdProperties = new ArrayList<Property>(getIdProperties().size() + 1);
100         newIdProperties.add(newIdProperty);
101         newIdProperties.addAll(getIdProperties());
102         return Location.create(newIdProperties);
103     }
104 
105     /**
106      * {@inheritDoc}
107      * 
108      * @see Location#with(Path)
109      */
110     @Override
111     public Location with( Path newPath ) {
112         if (newPath == null) return this;
113         Property idProperty = getIdProperties().get(0); // fast
114         return Location.create(newPath, idProperty);
115     }
116 
117     /**
118      * {@inheritDoc}
119      * 
120      * @see Location#with(UUID)
121      */
122     @Override
123     public Location with( UUID uuid ) {
124         if (uuid == null) return this;
125         Property idProperty = getIdProperties().get(0); // fast
126         if (ModeShapeLexicon.UUID.equals(idProperty.getName())) {
127             return Location.create(uuid);
128         }
129         List<Property> newIdProperties = new ArrayList<Property>(getIdProperties().size() + 1);
130         newIdProperties.add(new BasicSingleValueProperty(ModeShapeLexicon.UUID, uuid));
131         newIdProperties.addAll(getIdProperties());
132         return Location.create(newIdProperties);
133     }
134 }