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 Long} value of an attribute from/to an
28   * {@link Element}.
29   * 
30   * @author sanjin
31   */
32  public class LongAttribute extends Attribute<Long> {
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 Long} 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 LongAttribute(final String name, final Element element) {
52          super(name, element);
53  
54          this.name = name;
55      }
56  
57      @Override
58      protected String toString(final Long number) {
59          return String.valueOf((long) number);
60      }
61  
62      @Override
63      protected Long valueOf(final String str) throws InvalidFieldValueException {
64          final Long result;
65  
66          try {
67              result = Long.valueOf(str);
68          } catch (final NumberFormatException ex) {
69              throw new InvalidFieldValueException(MessageFormat.format(
70                      "{0} is not a number", this.name), ex);
71          }
72  
73          return result;
74      }
75  
76      /**
77       * Builder of {@link LongAttribute}.
78       * 
79       * @author sanjin
80       */
81      public static class Builder extends Attribute.Builder<Long> {
82  
83          /**
84           * Create an uninitialized instance of {@link LongAttribute.Builder}.
85           */
86          public Builder() {
87              super();
88          }
89  
90          /**
91           * Clone-constructor.
92           * 
93           * @param builder the cloned {@link LongAttribute.Builder}.
94           */
95          private Builder(final Builder builder) {
96              super(builder);
97          }
98  
99          /**
100          * Create instance of {@link LongAttribute}.
101          * 
102          * @return a {@link LongAttribute}.
103          */
104         @Override
105         public LongAttribute build() {
106             return new LongAttribute(getName(), getElement());
107         }
108 
109         @Override
110         protected Builder copy() {
111             return new Builder(this);
112         }
113     }
114 }