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.property.basic;
25  
26  import java.util.UUID;
27  import net.jcip.annotations.Immutable;
28  import org.modeshape.common.text.TextEncoder;
29  import org.modeshape.graph.property.Path;
30  import org.modeshape.graph.property.Reference;
31  
32  /**
33   * A {@link Reference} implementation that uses a single {@link UUID} as the pointer.
34   */
35  @Immutable
36  public class StringReference implements Reference {
37  
38      /**
39       */
40      private static final long serialVersionUID = 2299467578161645109L;
41      private/*final*/String id;
42      private/*final*/boolean isWeak;
43  
44      public StringReference( String id ) {
45          this.id = id;
46          this.isWeak = false;
47      }
48  
49      public StringReference( String id,
50                              boolean weak ) {
51          this.id = id;
52          this.isWeak = weak;
53      }
54  
55      /**
56       * {@inheritDoc}
57       * 
58       * @see org.modeshape.graph.property.Reference#isWeak()
59       */
60      public boolean isWeak() {
61          return isWeak;
62      }
63  
64      /**
65       * {@inheritDoc}
66       */
67      public String getString() {
68          return this.id;
69      }
70  
71      /**
72       * {@inheritDoc}
73       */
74      public String getString( TextEncoder encoder ) {
75          if (encoder == null) encoder = Path.DEFAULT_ENCODER;
76          return encoder.encode(getString());
77      }
78  
79      /**
80       * {@inheritDoc}
81       */
82      public int compareTo( Reference that ) {
83          if (this == that) return 0;
84          if (this.isWeak()) {
85              if (!that.isWeak()) return -1;
86          } else {
87              if (that.isWeak()) return 1;
88          }
89          if (that instanceof StringReference) {
90              return this.id.compareTo(((StringReference)that).getString());
91          }
92          return this.getString().compareTo(that.getString());
93      }
94  
95      /**
96       * {@inheritDoc}
97       */
98      @Override
99      public boolean equals( Object obj ) {
100         if (obj == this) return true;
101         if (obj instanceof Reference) {
102             Reference that = (Reference)obj;
103             return this.isWeak() == that.isWeak() && this.getString().equals(that.getString());
104         }
105         return super.equals(obj);
106     }
107 
108     /**
109      * {@inheritDoc}
110      */
111     @Override
112     public String toString() {
113         return this.id;
114     }
115 
116 }