View Javadoc
1   /*
2    * This file is part of Waarp Project (named also Waarp or GG).
3    *
4    *  Copyright (c) 2019, Waarp SAS, and individual contributors by the @author
5    *  tags. See the COPYRIGHT.txt in the distribution for a full listing of
6    * individual contributors.
7    *
8    *  All Waarp Project is free software: you can redistribute it and/or
9    * modify it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation, either version 3 of the License, or (at your
11   * option) any later version.
12   *
13   * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY
14   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15   * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License along with
18   * Waarp . If not, see <http://www.gnu.org/licenses/>.
19   */
20  package org.waarp.common.xml;
21  
22  import java.util.Enumeration;
23  import java.util.Hashtable;
24  import java.util.Set;
25  
26  /**
27   * XmlHash Hashtable for XmlValue utility. Hash all values (including subXml)
28   * but only root for Multiple
29   */
30  public class XmlHash {
31    final Hashtable<String, XmlValue> hashtable;
32  
33    XmlHash() {
34      hashtable = new Hashtable<String, XmlValue>();
35    }
36  
37    public XmlHash(final XmlValue[] values) {
38      hashtable = new Hashtable<String, XmlValue>();
39      for (final XmlValue xmlValue : values) {
40        if (xmlValue.isMultiple()) {
41          hashtable.put(xmlValue.getName(), xmlValue);
42        } else if (xmlValue.isSubXml()) {
43          put(xmlValue);
44        } else {
45          hashtable.put(xmlValue.getName(), xmlValue);
46        }
47      }
48    }
49  
50    public XmlHash(final XmlValue value) {
51      hashtable = new Hashtable<String, XmlValue>();
52      if (value == null) {
53        return;
54      }
55      if (value.isMultiple()) {
56        hashtable.put(value.getName(), value);
57      } else if (value.isSubXml()) {
58        put(value);
59      } else {
60        hashtable.put(value.getName(), value);
61      }
62    }
63  
64    public final XmlValue get(final String name) {
65      return hashtable.get(name);
66    }
67  
68    public final XmlValue put(final XmlValue value) {
69      if (value.isMultiple()) {
70        return hashtable.put(value.getName(), value);
71      } else if (value.isSubXml()) {
72        final XmlValue ret = hashtable.put(value.getName(), value);
73        if (!value.isEmpty()) {
74          for (final XmlValue subvalue : value.getSubXml()) {
75            if (subvalue != null) {
76              put(subvalue);
77            }
78          }
79        }
80        return ret;
81      } else {
82        return hashtable.put(value.getName(), value);
83      }
84    }
85  
86    public final int size() {
87      return hashtable.size();
88    }
89  
90    public final boolean isEmpty() {
91      return hashtable.isEmpty();
92    }
93  
94    public final Enumeration<String> keys() {
95      return hashtable.keys();
96    }
97  
98    public final Enumeration<XmlValue> elements() {
99      return hashtable.elements();
100   }
101 
102   public final boolean contains(final XmlValue value) {
103     return hashtable.contains(value);
104   }
105 
106   public final boolean containsValue(final XmlValue value) {
107     return hashtable.containsValue(value);
108   }
109 
110   public final boolean containsKey(final String key) {
111     return hashtable.containsKey(key);
112   }
113 
114   public final XmlValue remove(final String key) {
115     return hashtable.remove(key);
116   }
117 
118   public final void clear() {
119     hashtable.clear();
120   }
121 
122   public final Set<String> keySet() {
123     return hashtable.keySet();
124   }
125 
126 }