View Javadoc

1   /*
2     Copyright (c) 2003, Vilmantas Baranauskas
3     All rights reserved.
4   
5     Redistribution and use in source and binary forms, with or without
6     modification, are permitted provided that the following conditions are met:
7       * Redistributions of source code must retain the above copyright notice,
8         this list of conditions and the following disclaimer.
9       * Redistributions in binary form must reproduce the above copyright notice,
10        this list of conditions and the following disclaimer in the documentation
11        and/or other materials provided with the distribution.
12      * The names of its contributors may not be used to endorse or promote
13        products derived from this software without specific prior written
14        permission.
15  
16    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19    ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20    LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26    POSSIBILITY OF SUCH DAMAGE.
27  */
28  package org.sovt.impl;
29  
30  import org.sovt.Inspector;
31  import org.sovt.Transformer;
32  import org.sovt.ValidationResult;
33  import org.sovt.Validator;
34  
35  import java.util.ArrayList;
36  import java.util.Iterator;
37  import java.util.List;
38  
39  /***
40   * This Inspector is used to collect other validators, transformers or
41   * inspectors into a group.
42   *
43   * @author Vilmantas Baranauskas (vilmantas_baranauskas@yahoo.com)
44   */
45  public final class GroupInspector implements Inspector, Cloneable {
46     //-- constants -------------------------------------------------------------
47     //-- variables -------------------------------------------------------------
48     /***
49      * List of validators/transformers/inspectors in this group.
50      */
51     private List elements = new ArrayList();
52  
53     //-- constructors ----------------------------------------------------------
54     /***
55      * Creates instance of GroupValidator.
56      */
57     public GroupInspector() {
58     }
59  
60     //-- methods ---------------------------------------------------------------
61     /***
62      * Adds inspector to the group.
63      *
64      * @param inspector Inspector to add.
65      */
66     public void addInspector(Inspector inspector) {
67        elements.add(inspector);
68     }
69  
70     /***
71      * Adds transformer to the group.
72      *
73      * @param transformer Transformer to add.
74      */
75     public void addTransformer(Transformer transformer) {
76        elements.add(transformer);
77     }
78  
79     /***
80      * Adds validator to the group.
81      *
82      * @param validator Validator to add.
83      */
84     public void addValidator(Validator validator) {
85        elements.add(validator);
86     }
87  
88     /***
89      * Performs a partly deep-cloning. New list of contained elements is created
90      * but elements themselfs are not cloned.
91      *
92      * @return clone of itself.
93      * @throws CloneNotSupportedException never.
94      */
95     public Object clone() throws CloneNotSupportedException {
96        GroupInspector inspector = (GroupInspector) super.clone();
97        inspector.elements = new ArrayList(elements);
98        return inspector;
99     }
100 
101    /***
102     * Performs transformation and validation of given object. Transformers,
103     * validators and inspectors are invoked in the order they are registered.
104     *
105     * @param object Object to inspect.
106     * @param result Container for validation results.
107     * @return Transformed or completely new object which is also validated.
108     */
109    public Object inspect(Object object, ValidationResult result) {
110       for (Iterator iterator = elements.iterator(); iterator.hasNext();) {
111          Object element = iterator.next();
112          if (element instanceof Inspector) {
113             Inspector inspector = (Inspector) element;
114             object = inspector.inspect(object, result);
115          } else if (element instanceof Transformer) {
116             Transformer transformer = (Transformer) element;
117             object = transformer.transform(object);
118          } else if (element instanceof Validator) {
119             Validator validator = (Validator) element;
120             validator.validate(object, result);
121          }
122       }
123       return object;
124    }
125 
126 }