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.any23.extractor.microdata;
19  
20  /**
21   * Describes a <b>Microdata item property</b>.
22   *
23   * @author Michele Mostarda (mostarda@fbk.eu)
24   */
25  public class ItemProp extends Item {
26  
27      /**
28       * Property name.
29       */
30      private final String name;
31  
32      /**
33       * Property value.
34       */
35      private final ItemPropValue value;
36  
37      /**
38       * Constructor.
39       *
40       * @param xpath item location in container document.
41       * @param name item property name.
42       * @param value item property value.
43       */
44      public ItemProp(String xpath, String name, ItemPropValue value) {
45          this(xpath, name, value, false);
46      }
47  
48      final boolean reverse;
49      ItemProp(String xpath, String name, ItemPropValue value, boolean reverse) {
50          super(xpath);
51  
52          if(name == null) {
53              throw new NullPointerException("name cannot be null.");
54          }
55          if(name.trim().length() == 0) {
56              throw new IllegalArgumentException("invalid property name '" + name + "'");
57          }
58          if(value == null) {
59              throw new NullPointerException("value cannot be null.");
60          }
61          this.name = name;
62          this.value = value;
63          this.reverse = reverse;
64      }
65  
66      /**
67       * @return the item property name.
68       */
69      public String getName() {
70          return name;
71      }
72  
73      /**
74       * @return the item property value.
75       */
76      public ItemPropValue getValue() {
77          return value;
78      }
79  
80      @Override
81      public String toJSON() {
82          return String.format(
83                  "{ \"xpath\" : \"%s\", \"name\" : \"%s\", \"value\" : %s }",
84                  getXpath(),
85                  name,
86                  value.toJSON()
87          );
88      }
89  
90      @Override
91      public String toString() {
92          return toJSON();
93      }
94  
95      @Override
96      public int hashCode() {
97          return name.hashCode() * value.hashCode() * 3;
98      }
99  
100     @Override
101     public boolean equals(Object obj) {
102         if( obj == null ) {
103             return false;
104         }
105         if(obj == this) {
106             return true;
107         }
108         if(obj instanceof ItemProp) {
109             final ItemProp./../../../../org/apache/any23/extractor/microdata/ItemProp.html#ItemProp">ItemProp other = (ItemProp) obj;
110             return name.equals(other.name) && value.equals( other.value );
111         }
112         return false;
113     }
114 }