Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2010-2011 the original author or authors.
   *
   * 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.callbackparams.combine.annotation;
 
 import java.util.Map;
This is the Java5+ extension to org.callbackparams.combine.reflect.CallbackRecordsFactory to support the annotations @CombineConfig and @CallbackRecords. An instance of this class will be returned by org.callbackparams.combine.reflect.CallbackRecordsFactory.getInstance() when invoked inside a Java5+ JVM.

 
 
     @Override
     public CombineStrategy retrieveCombineStrategy(Class testClass) {
         final CombineConfig combineConfig = (CombineConfig)
                 testClass.getAnnotation(CombineConfig.class);
         
         if (null == combineConfig) {
             return super.retrieveCombineStrategy(testClass);
             
         } else {
             final CombineStrategy combineStrategy;
             try {
                 combineStrategy = combineConfig.strategy().newInstance();
             } catch (Exception x) {
                 throw new Error(x);
             }
             if (0 <= combineConfig.maxCount()) {
                 combineStrategy.setMaxCount(combineConfig.maxCount());
             }
             if (-1 != combineConfig.randomSeed()) {
                 combineStrategy.setRandomSeed(combineConfig.randomSeed());
             }
             return combineStrategy;
         }
     }

    
This overridden implementation looks for a method that is annotated with CallbackRecords and returns the records provided by that method. The method must be static or an Error will be thrown. If there is no such method or if the method is declared in a super-class and there is a CombineConfig annotation on a subclass then the callback-records will instead be determined in the combinatory manner. If the CallbackRecords annotated method's class is also annotated with CombineConfig then the callback-records will be collected by both strategies, i.e. combinatory-determined records will be collected as well as the records returned by the CallbackRecords method.
 
     @Override
     public Collection collectCallbackRecordsReflectively(final Class testClass) {
         final Class combineConfigAnnotatedSuperClass =
                 findCombineConfigAnnotatedSuperClass(testClass);
         
         Collection combinedRecords = null;
         for (Class c = testClass ; null == combinedRecords
                 ; c = c.getSuperclass()) {
             if (combineConfigAnnotatedSuperClass == c) {
                 combinedRecords = super
                         .collectCallbackRecordsReflectively(testClass);
             }
             
             for (Method m : c.getDeclaredMethods()) {
                 if (m.isAnnotationPresent(CallbackRecords.class)) {
                     try {
                         m.setAccessible(true);
                     } catch (SecurityException x) {
                        /* Never mind - we'll try to invoke it anyway ... */
                    }
                    try {
                        return new LazilyQueedCollection(
                                (Collectionm.invoke(null, (Object[])null),
                                combinedRecords);
                    } catch (Exception x) {
                        throw new Error("Failed to execute the @CallbackRecords "
                            + "annotated method. Make sure it is a static "
                            + "method!!"x);
                    }
                }
            }
        }
        
        return combinedRecords;
    }
    
    private Class findCombineConfigAnnotatedSuperClass(Class testSuperClass) {
        if (Object.class == testSuperClass) {
            return Object.class;
            
        } else {
            for (Annotation a : testSuperClass.getDeclaredAnnotations()) {
                if (a instanceof CombineConfig) {
                    return testSuperClass;
                }
            }
            return findCombineConfigAnnotatedSuperClass(
                    testSuperClass.getSuperclass());
        }
    }
    @Override
    protected Map<Field,Class<?>> collectValueInjections(final Class testClass) {
        Map injections = super.collectValueInjections(testClass);
        for (Class<?> c = testClassObject.class != cc = c.getSuperclass()) {
            for (Field f : c.getDeclaredFields()) {
                if (f.isAnnotationPresent(ParameterizedValue.class)) {
                    injections.put(ff.getType());
                }
            }
        }
        return injections;
    }
    @Override
    protected boolean isValueAlsoAvailableAsCallback(Field f) {
        if (false == super.isValueAlsoAvailableAsCallback(f)) {
            return false;
        } else {
            ParameterizedValue pv = f.getAnnotation(ParameterizedValue.class);
            return null == pv || pv.alsoAvailableAsCallback();
        }
    }

    
The collections passed to the constructor are combined in a lazy way. - Their iterators are not created until the iterators of the previous collections have been depleted.
    private static class LazilyQueedCollection
    extends AbstractCollection<Combined[]> {
        private final Collection[] que;
        LazilyQueedCollection(Collection... que) {
            this. = que;
        }
        public Iterator<Combined[]> iterator() {
            return new Iterator<Combined[]>() {
                Iterator currentIterator = ..iterator();
                int queIndex = -1;
                Object next;
                
                /* Initiate currentIterator, queIndex and next ... */
                {
                    prepareNext();
                }
                
                public boolean hasNext() {
                    return null != ;
                }
                public Combined[] next() {
                    try {
                        return asCombinedArray();
                        
                    } finally {
                        prepareNext();
                    }
                }
                public void remove() {
                    throw new UnsupportedOperationException("Not supported yet.");
                }
                private void prepareNext() {
                    while (false == .hasNext()
                            && ++ < .) {
                        if (null != []) {
                             = [].iterator();
                        }
                    }
                    if (.hasNext()) {
                         = .next();
                    } else {
                         = null;
                         = null;
                    }
                }
            };
        }
        public int size() {
            int size = 0;
            for (Collection c : ) {
                if (null != c) {
                    size += c.size();
                }
            }
            return size;
        }
    }
New to GrepCode? Check out our FAQ X