View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.jxpath.ri.model.beans;
19  
20  import java.util.Locale;
21  
22  import org.apache.commons.jxpath.JXPathContext;
23  import org.apache.commons.jxpath.ri.QName;
24  import org.apache.commons.jxpath.ri.model.NodePointer;
25  
26  /**
27   * Pointer whose value is {@code null}.
28   */
29  public class NullPointer extends PropertyOwnerPointer {
30  
31      private static final long serialVersionUID = 2193425983220679887L;
32  
33      /**
34       * The name of this node
35       */
36      private final QName qName;
37  
38      /**
39       * Optional ID, may be null.
40       */
41      private final String id;
42  
43      /**
44       * Constructs a new NullPointer.
45       *
46       * @param locale Locale.
47       * @param id     ID.
48       */
49      public NullPointer(final Locale locale, final String id) {
50          super(null, locale);
51          this.id = id;
52          this.qName = null;
53      }
54  
55      /**
56       * Used for the root node.
57       *
58       * @param parent parent pointer
59       * @param qName  node name
60       */
61      public NullPointer(final NodePointer parent, final QName qName) {
62          super(parent);
63          this.qName = qName;
64          this.id = null;
65      }
66  
67      /**
68       * Constructs a new NullPointer.
69       *
70       * @param qName  node name
71       * @param locale Locale
72       */
73      public NullPointer(final QName qName, final Locale locale) {
74          super(null, locale);
75          this.qName = qName;
76          this.id = null;
77      }
78  
79      @Override
80      public String asPath() {
81          if (id != null) {
82              return "id(" + id + ")";
83          }
84          return parent == null ? "null()" : super.asPath();
85      }
86  
87      @Override
88      public NodePointer createChild(final JXPathContext context, final QName qName, final int index) {
89          return createPath(context).createChild(context, qName, index);
90      }
91  
92      @Override
93      public NodePointer createChild(final JXPathContext context, final QName qName, final int index, final Object value) {
94          return createPath(context).createChild(context, qName, index, value);
95      }
96  
97      @Override
98      public NodePointer createPath(final JXPathContext context) {
99          if (parent != null) {
100             return parent.createPath(context).getValuePointer();
101         }
102         throw new UnsupportedOperationException("Cannot create the root object: " + asPath());
103     }
104 
105     @Override
106     public NodePointer createPath(final JXPathContext context, final Object value) {
107         if (parent != null) {
108             return parent.createPath(context, value).getValuePointer();
109         }
110         throw new UnsupportedOperationException("Cannot create the root object: " + asPath());
111     }
112 
113     @Override
114     public boolean equals(final Object object) {
115         if (object == this) {
116             return true;
117         }
118         if (!(object instanceof NullPointer)) {
119             return false;
120         }
121         final NullPointer other = (NullPointer) object;
122         return qName == other.qName || qName != null && qName.equals(other.qName);
123     }
124 
125     @Override
126     public Object getBaseValue() {
127         return null;
128     }
129 
130     @Override
131     public int getLength() {
132         return 0;
133     }
134 
135     @Override
136     public QName getName() {
137         return qName;
138     }
139 
140     @Override
141     public PropertyPointer getPropertyPointer() {
142         return new NullPropertyPointer(this);
143     }
144 
145     @Override
146     public int hashCode() {
147         return qName == null ? 0 : qName.hashCode();
148     }
149 
150     @Override
151     public boolean isActual() {
152         return false;
153     }
154 
155     @Override
156     public boolean isCollection() {
157         return false;
158     }
159 
160     @Override
161     public boolean isLeaf() {
162         return true;
163     }
164 }