View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */ 
17  package org.apache.commons.betwixt.strategy.impl;
18  
19  import java.util.ArrayList;
20  import java.util.Collection;
21  
22  import org.apache.commons.betwixt.strategy.CollectiveTypeStrategy;
23  
24  /**
25   * Strategy that allows specific classes to be marked as 
26   * collective ({@link #overrideCollective(Class)})
27   * or not collective ({@link #overrideNotCollective(Class)}).
28   * @since 0.8
29   */
30  public class OverrideCollectiveTypeStategy extends CollectiveTypeStrategy {
31  
32      private final CollectiveTypeStrategy delegate;
33      
34      private final Collection collectiveClasses;
35      private final Collection notCollectiveClasses;
36      
37      /**
38       * Constructs a strategy which delegates to CollectiveTypeStrategy#DEFAULT
39       *
40       */
41      public OverrideCollectiveTypeStategy() {
42          this(CollectiveTypeStrategy.DEFAULT);
43      }
44      
45      /**
46       * Constructs a strategy which delegates all those that it does not override.
47       * @param delegate
48       */
49      public OverrideCollectiveTypeStategy(CollectiveTypeStrategy delegate) {
50          super();
51          this.delegate = delegate;
52          collectiveClasses = new ArrayList();
53          notCollectiveClasses = new ArrayList();
54      }
55  
56      /**
57       * Marks the given type to be treated as collective.
58       * @param type <code>Class</code>, not null
59       */
60      public void overrideCollective(Class type) {
61          collectiveClasses.add(type);
62      }
63  
64      /**
65       * Marks the given type to be treated as not collective
66       * @param type
67       */
68      public void overrideNotCollective(Class type) {
69          notCollectiveClasses.add(type);
70      }
71      
72      /**
73       * @see CollectiveTypeStrategy#isCollective(Class)
74       */
75      public boolean isCollective(Class type) {
76          boolean result = delegate.isCollective(type);
77          if (collectiveClasses.contains(type)) {
78              result = true;
79          } else if (notCollectiveClasses.contains(type)) {
80              result = false;
81          }
82          return result;
83      }
84  
85  }