Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs + merging of similar code in FacadeImpl
git-svn-id: http://anonsvn.jboss.org/repos/weld/ri/trunk@468 1c488680-804c-0410-94cd-c6b725194a0e
- Loading branch information
Showing
4 changed files
with
126 additions
and
44 deletions.
There are no files selected for viewing
34 changes: 33 additions & 1 deletion
34
webbeans-ri/src/main/java/org/jboss/webbeans/CurrentManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 72 additions & 43 deletions
115
webbeans-ri/src/main/java/org/jboss/webbeans/FacadeImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,108 @@ | ||
/* | ||
* JBoss, Home of Professional Open Source | ||
* Copyright 2008, Red Hat Middleware LLC, and individual contributors | ||
* by the @authors tag. See the copyright.txt in the distribution for a | ||
* full listing of individual contributors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.jboss.webbeans; | ||
|
||
import java.lang.annotation.Annotation; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
import javax.webbeans.DuplicateBindingTypeException; | ||
import javax.webbeans.Observable; | ||
|
||
import org.jboss.webbeans.util.Reflections; | ||
|
||
public class FacadeImpl<T> { | ||
|
||
/** | ||
* Common implementation for binding-type-based helpers | ||
* | ||
* @author Gavin King | ||
* | ||
* @param <T> | ||
*/ | ||
public abstract class FacadeImpl<T> | ||
{ | ||
// The binding types the helper operates on | ||
protected final Set<? extends Annotation> bindingTypes; | ||
// The Web Beans manager | ||
protected final ManagerImpl manager; | ||
// The type of the operation | ||
protected final Class<T> type; | ||
|
||
/** | ||
* Validates the binding types | ||
* Constructor | ||
* | ||
* Removes @Observable from the list | ||
* | ||
* @param annotations The annotations to validate | ||
* @return A set of binding type annotations (minus @Observable, if it was | ||
* present) | ||
* @param manager The Web Beans manager | ||
* @param eventType The event type | ||
* @param bindingTypes The binding types | ||
*/ | ||
protected static Set<Annotation> getBindingTypes(Annotation... annotations) { | ||
Set<Annotation> result = new HashSet<Annotation>(); | ||
for (Annotation annotation : annotations) | ||
{ | ||
if (!Reflections.isBindingType(annotation)) | ||
{ | ||
throw new IllegalArgumentException(annotation + " is not a binding type"); | ||
} | ||
if (!annotation.annotationType().equals(Observable.class)) | ||
{ | ||
result.add(annotation); | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
protected FacadeImpl(ManagerImpl manager, Class<T> eventType, Annotation... bindingTypes) { | ||
protected FacadeImpl(ManagerImpl manager, Class<T> eventType, Annotation... bindingTypes) | ||
{ | ||
this.manager = manager; | ||
this.type = eventType; | ||
this.bindingTypes = getBindingTypes(bindingTypes); | ||
} | ||
this.bindingTypes = mergeBindingTypes(new HashSet<Annotation>(), bindingTypes); | ||
} | ||
|
||
/** | ||
* Validates the binding types and checks for duplicates among the annotations. | ||
* Merges and validates the current and new bindings | ||
* | ||
* Checkes with an abstract method for annotations to exclude | ||
* | ||
* @param annotations The annotations to validate | ||
* @return A set of unique binding type annotations | ||
* @param currentBindings Existing bindings | ||
* @param newBindings New bindings | ||
* @return The union of the bindings | ||
*/ | ||
protected Set<Annotation> checkBindingTypes(Annotation... annotations) { | ||
protected Set<Annotation> mergeBindingTypes(Set<? extends Annotation> currentBindings, Annotation... newBindings) | ||
{ | ||
Set<Annotation> result = new HashSet<Annotation>(); | ||
for (Annotation annotation : annotations) | ||
result.addAll(currentBindings); | ||
for (Annotation newAnnotation : newBindings) | ||
{ | ||
if (!Reflections.isBindingType(annotation)) | ||
if (!Reflections.isBindingType(newAnnotation)) | ||
{ | ||
throw new IllegalArgumentException(newAnnotation + " is not a binding type for " + this); | ||
} | ||
if (result.contains(newAnnotation)) | ||
{ | ||
throw new IllegalArgumentException(annotation + " is not a binding type for " + this); | ||
throw new DuplicateBindingTypeException(newAnnotation + " is already present in the bindings list for " + this); | ||
} | ||
if (result.contains(annotation) || this.bindingTypes.contains(annotation)) | ||
if (!getFilteredAnnotations().contains(newAnnotation.annotationType())) | ||
{ | ||
throw new DuplicateBindingTypeException(annotation + " is already present in the bindings list for " + this); | ||
result.add(newAnnotation); | ||
} | ||
result.add(annotation); | ||
} | ||
return result; | ||
} | ||
|
||
protected Annotation[] mergeBindings(Annotation... bindingTypes) { | ||
Set<Annotation> bindingParameters = checkBindingTypes(bindingTypes); | ||
bindingParameters.addAll(this.bindingTypes); | ||
Annotation[] bindings = bindingParameters.toArray(new Annotation[0]); | ||
return bindings; | ||
/** | ||
* Gets a set of annotation classes to ignore | ||
* | ||
* @return A set of annotation classes to ignore | ||
*/ | ||
protected abstract Set<Class<? extends Annotation>> getFilteredAnnotations(); | ||
|
||
/** | ||
* Merges the binding this helper operates upon with the parameters | ||
* | ||
* @param bindingTypes The bindings to merge | ||
* | ||
* @return The union of the binding types | ||
*/ | ||
protected Annotation[] mergeBindings(Annotation... newBindingTypes) | ||
{ | ||
return mergeBindingTypes(bindingTypes, newBindingTypes).toArray(new Annotation[0]); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters