Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2011 JBoss Inc
   *
   * 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.drools.planner.core.domain.entity;
 
 import java.util.Map;
 
 
 public class PlanningEntityDescriptor {
 
     private final SolutionDescriptor solutionDescriptor;
 
     private final Class<?> planningEntityClass;
     private final BeanInfo planningEntityBeanInfo;
 
 
     public PlanningEntityDescriptor(SolutionDescriptor solutionDescriptorClass<?> planningEntityClass) {
         this. = solutionDescriptor;
         this. = planningEntityClass;
         try {
              = Introspector.getBeanInfo(planningEntityClass);
         } catch (IntrospectionException e) {
             throw new IllegalStateException("The planningEntityClass (" + planningEntityClass
                     + ") is not a valid java bean."e);
         }
         processEntityAnnotations();
         processPropertyAnnotations();
     }
 
     private void processEntityAnnotations() {
         PlanningEntity planningEntityAnnotation = .getAnnotation(PlanningEntity.class);
         if (planningEntityAnnotation == null) {
             throw new IllegalStateException("The planningEntityClass (" + 
                     + ") has been specified as a planning entity in the configuration," +
                     " but does not have a PlanningEntity annotation.");
         }
         Class<? extends ComparatordifficultyComparatorClass = planningEntityAnnotation.difficultyComparatorClass();
         if (difficultyComparatorClass == PlanningEntity.NullDifficultyComparator.class) {
             difficultyComparatorClass = null;
         }
         Class<? extends PlanningEntityDifficultyWeightFactorydifficultyWeightFactoryClass
                 = planningEntityAnnotation.difficultyWeightFactoryClass();
         if (difficultyWeightFactoryClass == PlanningEntity.NullDifficultyWeightFactory.class) {
             difficultyWeightFactoryClass = null;
         }
         if (difficultyComparatorClass != null && difficultyWeightFactoryClass != null) {
             throw new IllegalStateException("The planningEntityClass (" + 
                     + ") cannot have a difficultyComparatorClass (" + difficultyComparatorClass.getName()
                     + ") and a difficultyWeightFactoryClass (" + difficultyWeightFactoryClass.getName()
                     + ") at the same time.");
         }
          = new PlanningEntitySorter();
         if (difficultyComparatorClass != null) {
             Comparator<ObjectdifficultyComparator;
             try {
                 difficultyComparator = difficultyComparatorClass.newInstance();
             } catch (InstantiationException e) {
                 throw new IllegalArgumentException("The difficultyComparatorClass ("
                         + difficultyComparatorClass.getName()
                         + ") does not have a public no-arg constructor"e);
             } catch (IllegalAccessException e) {
                 throw new IllegalArgumentException("The difficultyComparatorClass ("
                         + difficultyComparatorClass.getName()
                         + ") does not have a public no-arg constructor"e);
             }
             .setDifficultyComparator(difficultyComparator);
         }
         if (difficultyWeightFactoryClass != null) {
             PlanningEntityDifficultyWeightFactory difficultyWeightFactory;
             try {
                 difficultyWeightFactory = difficultyWeightFactoryClass.newInstance();
            } catch (InstantiationException e) {
                throw new IllegalArgumentException("The difficultyWeightFactoryClass ("
                        + difficultyWeightFactoryClass.getName()
                        + ") does not have a public no-arg constructor"e);
            } catch (IllegalAccessException e) {
                throw new IllegalArgumentException("The difficultyWeightFactoryClass ("
                        + difficultyWeightFactoryClass.getName()
                        + ") does not have a public no-arg constructor"e);
            }
            .setDifficultyWeightFactory(difficultyWeightFactory);
        }
    }
    private void processPropertyAnnotations() {
        int mapSize = .getPropertyDescriptors().length;
        boolean noPlanningVariableAnnotation = true;
        for (PropertyDescriptor propertyDescriptor : .getPropertyDescriptors()) {
            Method propertyGetter = propertyDescriptor.getReadMethod();
            if (propertyGetter.isAnnotationPresent(PlanningVariable.class)) {
                noPlanningVariableAnnotation = false;
                if (propertyDescriptor.getWriteMethod() == null) {
                    throw new IllegalStateException("The planningEntityClass (" + 
                            + ") has a PlanningVariable annotated property (" + propertyDescriptor.getName()
                            + ") that should have a setter.");
                }
                PlanningVariableDescriptor planningVariableDescriptor = new PlanningVariableDescriptor(
                        thispropertyDescriptor);
                .put(propertyDescriptor.getName(), planningVariableDescriptor);
            }
        }
        if (noPlanningVariableAnnotation) {
            throw new IllegalStateException("The planningEntityClass (" + 
                    + ") should have at least 1 getter with a PlanningVariable annotation.");
        }
    }
        return ;
    }
    public Class<?> getPlanningEntityClass() {
        return ;
    }
        return ;
    }
    public PropertyDescriptor getPropertyDescriptor(String propertyName) {
        for (PropertyDescriptor propertyDescriptor : .getPropertyDescriptors()) {
            if (propertyDescriptor.getName().equals(propertyName)) {
                return propertyDescriptor;
            }
        }
        return null;
    }
        return .values();
    }
    public boolean isInitialized(Object planningEntity) {
        for (PlanningVariableDescriptor planningVariableDescriptor : .values()) {
            if (!planningVariableDescriptor.isInitialized(planningEntity)) {
                return false;
            }
        }
        return true;
    }
    public void uninitialize(Object planningEntity) {
        for (PlanningVariableDescriptor planningVariableDescriptor : .values()) {
            planningVariableDescriptor.uninitialize(planningEntity);
        }
    }
New to GrepCode? Check out our FAQ X