Home | History | Annotate | Download | only in configuration
      1 /*
      2  * Copyright (C) 2007 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.ide.common.resources.configuration;
     18 
     19 
     20 /**
     21  * Base class for resource qualifiers.
     22  * <p/>The resource qualifier classes are designed as immutable.
     23  */
     24 public abstract class ResourceQualifier implements Comparable<ResourceQualifier> {
     25 
     26     /**
     27      * Returns the human readable name of the qualifier.
     28      */
     29     public abstract String getName();
     30 
     31     /**
     32      * Returns a shorter human readable name for the qualifier.
     33      * @see #getName()
     34      */
     35     public abstract String getShortName();
     36 
     37     /**
     38      * Returns when this qualifier was added to Android.
     39      */
     40     public abstract int since();
     41 
     42     /**
     43      * Whether this qualifier is deprecated.
     44      */
     45     public boolean deprecated() {
     46         return false;
     47     }
     48 
     49     /**
     50      * Returns whether the qualifier has a valid filter value.
     51      */
     52     public abstract boolean isValid();
     53 
     54     /**
     55      * Returns whether the qualifier has a fake value.
     56      * <p/>Fake values are used internally and should not be used as real qualifier value.
     57      */
     58     public abstract boolean hasFakeValue();
     59 
     60     /**
     61      * Check if the value is valid for this qualifier, and if so sets the value
     62      * into a Folder Configuration.
     63      * @param value The value to check and set. Must not be null.
     64      * @param config The folder configuration to receive the value. Must not be null.
     65      * @return true if the value was valid and was set.
     66      */
     67     public abstract boolean checkAndSet(String value, FolderConfiguration config);
     68 
     69     /**
     70      * Returns a string formated to be used in a folder name.
     71      * <p/>This is declared as abstract to force children classes to implement it.
     72      */
     73     public abstract String getFolderSegment();
     74 
     75     /**
     76      * Returns whether the given qualifier is a match for the receiver.
     77      * <p/>The default implementation returns the result of {@link #equals(Object)}.
     78      * <p/>Children class that re-implements this must implement
     79      * {@link #isBetterMatchThan(ResourceQualifier, ResourceQualifier)} too.
     80      * @param qualifier the reference qualifier
     81      * @return true if the receiver is a match.
     82      */
     83     public boolean isMatchFor(ResourceQualifier qualifier) {
     84         return equals(qualifier);
     85     }
     86 
     87     /**
     88      * Returns true if the receiver is a better match for the given <var>reference</var> than
     89      * the given <var>compareTo</var> comparable.
     90      * @param compareTo The {@link ResourceQualifier} to compare to. Can be null, in which
     91      * case the method must return <code>true</code>.
     92      * @param reference The reference qualifier value for which the match is.
     93      * @return true if the receiver is a better match.
     94      */
     95     public boolean isBetterMatchThan(ResourceQualifier compareTo, ResourceQualifier reference) {
     96         // the default is to always return false. This gives less overhead than always returning
     97         // true, as it would only compare same values anyway.
     98         return false;
     99     }
    100 
    101     @Override
    102     public String toString() {
    103         return getFolderSegment();
    104     }
    105 
    106     /**
    107      * Returns a string formatted for display purpose.
    108      */
    109     public abstract String getShortDisplayValue();
    110 
    111     /**
    112      * Returns a string formatted for display purpose.
    113      */
    114     public abstract String getLongDisplayValue();
    115 
    116     /**
    117      * Returns <code>true</code> if both objects are equal.
    118      * <p/>This is declared as abstract to force children classes to implement it.
    119      */
    120     @Override
    121     public abstract boolean equals(Object object);
    122 
    123     /**
    124      * Returns a hash code value for the object.
    125      * <p/>This is declared as abstract to force children classes to implement it.
    126      */
    127     @Override
    128     public abstract int hashCode();
    129 
    130     @Override
    131     public final int compareTo(ResourceQualifier o) {
    132         return toString().compareTo(o.toString());
    133     }
    134 }
    135