View Javadoc

1   /* This file is part of COPAL (COntext Provisioning for All).
2    *
3    * COPAL is a part of SM4All (Smart hoMes for All) project.
4    *
5    * COPAL is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU Lesser General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * COPAL is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public License
16   * along with COPAL. If not, see <http://www.gnu.org/licenses/>.
17   */
18  package at.ac.tuwien.infosys.sm4all.copal.api.xml;
19  
20  import java.text.MessageFormat;
21  import org.w3c.dom.Element;
22  import at.ac.tuwien.infosys.sm4all.copal.api.util.InvalidFieldValueException;
23  import at.ac.tuwien.infosys.sm4all.copal.api.util.Marshaller;
24  import at.ac.tuwien.infosys.sm4all.copal.api.util.Unmarshaller;
25  
26  /**
27   * Unmarshals and marshals a {@link Integer} value of an attribute from/to an
28   * {@link Element}.
29   * 
30   * @author sanjin
31   */
32  public class IntegerAttribute extends Attribute<Integer> {
33  
34      private final String name;
35  
36      /**
37       * Creates instance of an attribute {@link Unmarshaller} and
38       * {@link Marshaller} which uses specified {@link Element} to unmarshal
39       * and/or marshal the {@link Integer} value of the attribute with specified
40       * name.
41       * 
42       * @param name the name of the attribute in the {@link Element} used for
43       *        unmarshalling and marshalling.
44       * @param element the {@link Element} used for unmarshalling and
45       *        marshalling.
46       * @throws NullPointerException if specified {@link Element} or name is
47       *         <code>null</code>.
48       * @throws IllegalArgumentException if specified name is an empty or blank
49       *         string.
50       */
51      public IntegerAttribute(final String name, final Element element) {
52          super(name, element);
53  
54          this.name = name;
55      }
56  
57      @Override
58      protected String toString(final Integer number) {
59          return String.valueOf((int) number);
60      }
61  
62      @Override
63      protected Integer valueOf(final String str)
64              throws InvalidFieldValueException {
65          final Integer result;
66  
67          try {
68              result = Integer.valueOf(str);
69          } catch (final NumberFormatException ex) {
70              throw new InvalidFieldValueException(MessageFormat.format(
71                      "{0} is not a number", this.name), ex);
72          }
73  
74          return result;
75      }
76  
77      /**
78       * Builder of {@link IntegerAttribute}.
79       * 
80       * @author sanjin
81       */
82      public static class Builder extends Attribute.Builder<Integer> {
83  
84          /**
85           * Create an uninitialized instance of {@link IntegerAttribute.Builder}.
86           */
87          public Builder() {
88              super();
89          }
90  
91          /**
92           * Clone-constructor.
93           * 
94           * @param builder the cloned {@link IntegerAttribute.Builder}.
95           */
96          private Builder(final Builder builder) {
97              super(builder);
98          }
99  
100         /**
101          * Create instance of {@link IntegerAttribute}.
102          * 
103          * @return a {@link IntegerAttribute}.
104          */
105         @Override
106         public IntegerAttribute build() {
107             return new IntegerAttribute(getName(), getElement());
108         }
109 
110         @Override
111         protected Builder copy() {
112             return new Builder(this);
113         }
114     }
115 }