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.Collections;
27  import java.util.UUID;
28  import net.jcip.annotations.Immutable;
29  import org.modeshape.common.util.CheckArg;
30  import org.modeshape.graph.property.Name;
31  import org.modeshape.graph.property.Path;
32  import org.modeshape.graph.property.Property;
33  import org.modeshape.graph.property.basic.BasicSingleValueProperty;
34  
35  /**
36   * General purpose location type that supports a path and a property. This class should never be directly instantiated by users of
37   * the ModeShape framework. Instead, use @{link Location#create} to create the correct location.
38   * 
39   * @see Location
40   */
41  @Immutable
42  class LocationWithPathAndProperty extends LocationWithPathAndProperties {
43  
44      private static final long serialVersionUID = 1L;
45  
46      /**
47       * Create a new location with a given path and identification property.
48       * 
49       * @param path the path
50       * @param idProperty the identification property
51       */
52      LocationWithPathAndProperty( Path path,
53                                   Property idProperty ) {
54          super(path, 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 org.modeshape.graph.LocationWithPathAndProperties#hasIdProperties()
91       */
92      @Override
93      public final boolean hasIdProperties() {
94          return true;
95      }
96  
97      /**
98       * {@inheritDoc}
99       * 
100      * @see Location#with(Property)
101      */
102     @Override
103     public Location with( Property newIdProperty ) {
104         if (newIdProperty == null || newIdProperty.isEmpty()) return this;
105         Property idProperty = getIdProperties().get(0); // fast
106         if (newIdProperty.getName().equals(idProperty.getName())) {
107             return Location.create(getPath(), newIdProperty);
108         }
109         return Location.create(getPath(), idProperty, newIdProperty);
110     }
111 
112     /**
113      * {@inheritDoc}
114      * 
115      * @see Location#with(Path)
116      */
117     @Override
118     public Location with( Path newPath ) {
119         if (newPath == null) return Location.create(getIdProperties());
120         if (getPath().equals(newPath)) return this;
121         Property idProperty = getIdProperties().get(0); // fast
122         return Location.create(newPath, idProperty);
123     }
124 
125     /**
126      * {@inheritDoc}
127      * 
128      * @see Location#with(UUID)
129      */
130     @Override
131     public Location with( UUID uuid ) {
132         Property idProperty = getIdProperties().get(0); // fast
133         if (uuid == null) return Location.create(getPath());
134         assert !ModeShapeLexicon.UUID.equals(idProperty.getName());
135         Property newUuidProperty = new BasicSingleValueProperty(ModeShapeLexicon.UUID, uuid);
136         return Location.create(getPath(), idProperty, newUuidProperty);
137     }
138 }