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.util.concurrent.atomic.AtomicReference;
21  import org.w3c.dom.Element;
22  import at.ac.tuwien.infosys.sm4all.copal.api.util.Marshaller;
23  
24  /**
25   * Base abstract class for implementations of {@link MarshallerBuilder} to use.
26   * 
27   * @param <T> the type of marshaled value.
28   * @author sanjin
29   */
30  public abstract class BaseMarshallerBuilder<T> implements MarshallerBuilder<T> {
31  
32      private final AtomicReference<Element> elementRef = new AtomicReference<Element>();
33  
34      /**
35       * Create uninitialized instance of {@link BaseMarshallerBuilder}.
36       */
37      protected BaseMarshallerBuilder() {
38          super();
39      }
40  
41      /**
42       * Clone-constructor.
43       * 
44       * @param builder the cloned {@link BaseMarshallerBuilder}.
45       */
46      protected BaseMarshallerBuilder(final BaseMarshallerBuilder<T> builder) {
47          this();
48  
49          this.elementRef.set(builder.elementRef.get());
50      }
51  
52      /**
53       * Create instance of {@link BaseMarshallerBuilder} by copying this
54       * {@link BaseMarshallerBuilder}.
55       * 
56       * @return an {@link BaseMarshallerBuilder}.
57       */
58      protected abstract BaseMarshallerBuilder<T> copy();
59  
60      /**
61       * Returns the {@link Element}.
62       * 
63       * @return the {@link Element}.
64       */
65      public Element getElement() {
66          return this.elementRef.get();
67      }
68  
69      /**
70       * Create instance of {@link BaseMarshallerBuilder} that will build
71       * {@link Marshaller}s that will marshal values to specified {@link Element}
72       * .
73       * 
74       * @param element the {@link Element}.
75       * @return an {@link BaseMarshallerBuilder}.
76       */
77      @Override
78      public BaseMarshallerBuilder<T> withElement(final Element element) {
79          final BaseMarshallerBuilder<T> result = copy();
80  
81          result.elementRef.set(element);
82  
83          return result;
84      }
85  }