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 net.jcip.annotations.Immutable;
27  import org.modeshape.common.text.TextEncoder;
28  import org.modeshape.graph.property.Name;
29  import org.modeshape.graph.property.NamespaceRegistry;
30  import org.modeshape.graph.property.Path;
31  
32  /**
33   * A {@link Path.Segment} implementation that represents an identifier segment.
34   */
35  @Immutable
36  public class IdentifierPathSegment extends BasicPathSegment {
37  
38      private static final long serialVersionUID = 1L;
39  
40      public IdentifierPathSegment( Name name ) {
41          super(name);
42      }
43  
44      /**
45       * {@inheritDoc}
46       * 
47       * @see org.modeshape.graph.property.Path.Segment#isIdentifier()
48       */
49      @Override
50      public boolean isIdentifier() {
51          return true;
52      }
53  
54      /**
55       * {@inheritDoc}
56       */
57      @Override
58      public boolean equals( Object obj ) {
59          if (obj == this) return true;
60          if (obj instanceof Path.Segment) {
61              Path.Segment that = (Path.Segment)obj;
62              if (!this.getName().equals(that.getName())) return false;
63              return Math.abs(getIndex()) == Math.abs(that.getIndex());
64          }
65          return false;
66      }
67  
68      /**
69       * {@inheritDoc}
70       */
71      @Override
72      public String toString() {
73          if (this.hasIndex()) {
74              return this.getName().toString() + "[" + this.getIndex() + "]";
75          }
76          return this.getName().toString();
77      }
78  
79      /**
80       * {@inheritDoc}
81       */
82      @Override
83      public String getString( TextEncoder encoder ) {
84          if (encoder == null) encoder = Path.DEFAULT_ENCODER;
85          StringBuilder sb = new StringBuilder();
86          sb.append('[').append(this.getName().getString(encoder)).append(']');
87          return sb.toString();
88      }
89  
90      /**
91       * {@inheritDoc}
92       * 
93       * @see org.modeshape.graph.property.Path.Segment#getString(org.modeshape.graph.property.NamespaceRegistry,
94       *      org.modeshape.common.text.TextEncoder, org.modeshape.common.text.TextEncoder)
95       */
96      @Override
97      public String getString( NamespaceRegistry namespaceRegistry,
98                               TextEncoder encoder,
99                               TextEncoder delimiterEncoder ) {
100         if (encoder == null) encoder = Path.DEFAULT_ENCODER;
101         StringBuilder sb = new StringBuilder();
102         sb.append('[').append(this.getName().getString(namespaceRegistry, encoder, delimiterEncoder)).append(']');
103         return sb.toString();
104     }
105 }