d7d8070ca00fff7d311288e1f14db4bd3b63d2cb
[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 // TODO implement copying of routerconfig. 
26
27 /**
28  * Represents a single configuration set of a single configuration client of the
29  * XML router.
30  * 
31  * @author Erik Brakkee
32  */
33 public class SingleRouterConfig implements ExtendedRouterConfig {
34
35     public static final class DocumentConfig extends ConfigImpl<DocumentType> {
36         public DocumentConfig(Id<Config> aId) {
37             super(DocumentType.class, aId);
38         }
39
40         public DocumentConfig(DocumentConfig aConfig) {
41             super(aConfig);
42         }
43
44         @Override
45         public DocumentType wrap(DocumentType aT) {
46             return new RobustDocumentType(aT);
47         }
48     }
49
50     public static final class TransformationConfig extends
51         ConfigImpl<Transformation> {
52         public TransformationConfig(Id<Config> aId) {
53             super(Transformation.class, aId);
54         }
55
56         public TransformationConfig(TransformationConfig aConfig) {
57             super(aConfig);
58         }
59
60         @Override
61         public Transformation wrap(Transformation aTransformation) {
62             return new RobustTransformation(aTransformation);
63         }
64     }
65
66     public static final class FilterConfig extends ConfigImpl<Filter> {
67         public FilterConfig(Id<Config> aId) {
68             super(Filter.class, aId);
69         }
70
71         public FilterConfig(FilterConfig aConfig) {
72             super(aConfig);
73         }
74
75         @Override
76         public Filter wrap(Filter aFilter) {
77             return new RobustFilter("", aFilter);
78         }
79     }
80
81     private Id<RouterConfig> id;
82
83     private DocumentConfig documentTypes;
84     private TransformationConfig transformations;
85     private FilterConfig filters;
86
87     /**
88      * Constructs a router configuration.
89      * 
90      * @param aId
91      *            Unique id for this configuration.
92      */
93     public SingleRouterConfig(Id<RouterConfig> aId) {
94         id = aId;
95         documentTypes = new DocumentConfig(new Id<Config>(aId.getId() +
96             ".documenttypes"));
97         transformations = new TransformationConfig(new Id<Config>(aId.getId() +
98             ".transformations"));
99         filters = new FilterConfig(new Id<Config>(aId.getId() + ".filters"));
100     }
101
102     public SingleRouterConfig(SingleRouterConfig aConfig) {
103         id = aConfig.id;
104         documentTypes = new DocumentConfig(aConfig.documentTypes);
105         transformations = new TransformationConfig(aConfig.transformations);
106         filters = new FilterConfig(aConfig.filters);
107     }
108
109     @Override
110     public Id<RouterConfig> getId() {
111         return id;
112     }
113
114     @Override
115     public Config<DocumentType> documentTypeConfig() {
116         return documentTypes;
117     }
118
119     @Override
120     public Config<Transformation> transformationConfig() {
121         return transformations;
122     }
123
124     @Override
125     public Config<Filter> filterConfig() {
126         return filters;
127     }
128
129     @Override
130     public boolean equals(Object aObj) {
131         if (aObj == null) {
132             return false;
133         }
134         if (!(aObj instanceof SingleRouterConfig)) {
135             return false;
136         }
137         SingleRouterConfig obj = (SingleRouterConfig) aObj;
138
139         return documentTypes.equals(obj.documentTypes) &&
140             transformations.equals(obj.transformations) &&
141             filters.equals(obj.filters);
142     }
143
144     @Override
145     public int hashCode() {
146         return documentTypes.hashCode() + transformations.hashCode() +
147             filters.hashCode();
148     }
149 }