a72e9a2294d6c49a4d51c3a621c4f4460ac80d9d
[xmlrouter] / impl / src / main / java / org / wamblee / xmlrouter / impl / SingleRouterConfig.java
1 /*
2  * Copyright 2005-2011 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.wamblee.xmlrouter.impl;
17
18 import org.wamblee.xmlrouter.common.Id;
19 import org.wamblee.xmlrouter.config.Config;
20 import org.wamblee.xmlrouter.config.DocumentType;
21 import org.wamblee.xmlrouter.config.Filter;
22 import org.wamblee.xmlrouter.config.RouterConfig;
23 import org.wamblee.xmlrouter.config.Transformation;
24
25 /**
26  * Represents a single configuration set of a single configuration client of the
27  * XML router.
28  * 
29  * @author Erik Brakkee
30  */
31 public class SingleRouterConfig implements ExtendedRouterConfig {
32
33     public static final class DocumentConfig extends ConfigImpl<DocumentType> {
34         public DocumentConfig(Id<Config> aId) {
35             super(DocumentType.class, aId);
36         }
37
38         public DocumentConfig(DocumentConfig aConfig) {
39             super(aConfig);
40         }
41
42         @Override
43         public DocumentType wrap(DocumentType aT) {
44             return new RobustDocumentType(aT);
45         }
46     }
47
48     public static final class TransformationConfig extends
49         ConfigImpl<Transformation> {
50         public TransformationConfig(Id<Config> aId) {
51             super(Transformation.class, aId);
52         }
53
54         public TransformationConfig(TransformationConfig aConfig) {
55             super(aConfig);
56         }
57
58         @Override
59         public Transformation wrap(Transformation aTransformation) {
60             return new RobustTransformation(aTransformation);
61         }
62     }
63
64     public static final class FilterConfig extends ConfigImpl<Filter> {
65         public FilterConfig(Id<Config> aId) {
66             super(Filter.class, aId);
67         }
68
69         public FilterConfig(FilterConfig aConfig) {
70             super(aConfig);
71         }
72
73         @Override
74         public Filter wrap(Filter aFilter) {
75             return new RobustFilter("", aFilter);
76         }
77     }
78
79     private Id<RouterConfig> id;
80
81     private DocumentConfig documentTypes;
82     private TransformationConfig transformations;
83     private FilterConfig filters;
84
85     /**
86      * Constructs a router configuration.
87      * 
88      * @param aId
89      *            Unique id for this configuration.
90      */
91     public SingleRouterConfig(Id<RouterConfig> aId) {
92         id = aId;
93         documentTypes = new DocumentConfig(new Id<Config>(aId.getId() +
94             ".documenttypes"));
95         transformations = new TransformationConfig(new Id<Config>(aId.getId() +
96             ".transformations"));
97         filters = new FilterConfig(new Id<Config>(aId.getId() + ".filters"));
98     }
99
100     public SingleRouterConfig(SingleRouterConfig aConfig) {
101         id = aConfig.id;
102         documentTypes = new DocumentConfig(aConfig.documentTypes);
103         transformations = new TransformationConfig(aConfig.transformations);
104         filters = new FilterConfig(aConfig.filters);
105     }
106
107     @Override
108     public Id<RouterConfig> getId() {
109         return id;
110     }
111
112     @Override
113     public Config<DocumentType> documentTypeConfig() {
114         return documentTypes;
115     }
116
117     @Override
118     public Config<Transformation> transformationConfig() {
119         return transformations;
120     }
121
122     @Override
123     public Config<Filter> filterConfig() {
124         return filters;
125     }
126
127     @Override
128     public boolean equals(Object aObj) {
129         if (aObj == null) {
130             return false;
131         }
132         if (!(aObj instanceof SingleRouterConfig)) {
133             return false;
134         }
135         SingleRouterConfig obj = (SingleRouterConfig) aObj;
136
137         return documentTypes.equals(obj.documentTypes) &&
138             transformations.equals(obj.transformations) &&
139             filters.equals(obj.filters);
140     }
141
142     @Override
143     public int hashCode() {
144         return documentTypes.hashCode() + transformations.hashCode() +
145             filters.hashCode();
146     }
147 }