Home | History | Annotate | Download | only in config
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      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 
     17 package com.android.tradefed.config;
     18 
     19 import java.lang.reflect.Field;
     20 import java.util.Collection;  // imported for javadoc
     21 import java.util.Map;  // imported for javadoc
     22 
     23 /**
     24  * Controls the behavior when an option is specified multiple times.  Note that this enum assumes
     25  * that the values to be set are not {@link Collection}s or {@link Map}s.
     26  */
     27 public enum OptionUpdateRule {
     28     /** once an option is set, subsequent attempts to update it should be ignored. */
     29     FIRST {
     30         @Override
     31         boolean shouldUpdate(String optionName, Object current, Object update)
     32                 throws ConfigurationException {
     33             return current == null;
     34         }
     35     },
     36 
     37     /** if an option is set multiple times, ignore all but the last value. */
     38     LAST {
     39         @Override
     40         boolean shouldUpdate(String optionName, Object current, Object update)
     41                 throws ConfigurationException {
     42             return true;
     43         }
     44     },
     45 
     46     /** for {@link Comparable} options, keep the one that compares as the greatest. */
     47     GREATEST {
     48         @Override
     49         boolean shouldUpdate(String optionName, Object current, Object update)
     50                 throws ConfigurationException {
     51             return current == null || compare(optionName, current, update) < 0;
     52         }
     53     },
     54 
     55     /** for {@link Comparable} options, keep the one that compares as the least. */
     56     LEAST {
     57         @Override
     58         boolean shouldUpdate(String optionName, Object current, Object update)
     59                 throws ConfigurationException {
     60             return current == null || compare(optionName, current, update) > 0;
     61         }
     62     },
     63 
     64     /** throw a {@link ConfigurationException} if this option is set more than once. */
     65     IMMUTABLE {
     66         @Override
     67         boolean shouldUpdate(String optionName, Object current, Object update)
     68                 throws ConfigurationException {
     69 
     70             if (current != null) {
     71                 throw new ConfigurationException(String.format(
     72                         "Attempted to update immutable value (%s) for option \"%s\"", optionName,
     73                         optionName));
     74             }
     75             return true;
     76         }
     77     };
     78 
     79     abstract boolean shouldUpdate(String optionName, Object current, Object update)
     80             throws ConfigurationException;
     81 
     82     /**
     83       * Takes the current value and the update value, and returns whether the value should be
     84       * updated.  Assumes that <code>update</code> is never null.
     85       */
     86     public boolean shouldUpdate(String optionName, Object optionSource, Field field, Object update)
     87             throws ConfigurationException {
     88         Object current;
     89         try {
     90             current = field.get(optionSource);
     91         } catch (IllegalAccessException e) {
     92             throw new ConfigurationException(String.format(
     93                     "internal error when setting option '%s'", optionName), e);
     94         }
     95         return shouldUpdate(optionName, current, update);
     96     }
     97 
     98     /**
     99      * Check if the objects are {@link Comparable}, and if so, compare them using
    100      * {@link Comparable#compareTo(Object)}
    101      */
    102     @SuppressWarnings({"unchecked", "rawtypes"})
    103     private static int compare(String optionName, Object current, Object update)
    104             throws ConfigurationException {
    105         Comparable compCurrent;
    106         if (current instanceof Comparable) {
    107             compCurrent = (Comparable) current;
    108         } else {
    109             throw new ConfigurationException(String.format(
    110                     "internal error: Class %s for option %s was used with GREATEST or LEAST " +
    111                     "updateRule, but does not implement Comparable.",
    112                     current.getClass().getSimpleName(), optionName));
    113         }
    114 
    115         try {
    116             return compCurrent.compareTo(update);
    117         } catch (ClassCastException e) {
    118             throw new ConfigurationException(String.format(
    119                     "internal error: Failed to compare %s (%s) and %s (%s)",
    120                     current.getClass().getName(), current, update.getClass().getName(), update), e);
    121         }
    122     }
    123 }
    124 
    125