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 import java.util.regex.Matcher;
     20 import java.util.regex.Pattern;
     21 
     22 /**
     23  * Resource Qualifier for Region.
     24  */
     25 public final class RegionQualifier extends ResourceQualifier {
     26     private final static Pattern sRegionPattern = Pattern.compile("^r([A-Z]{2})$"); //$NON-NLS-1$
     27 
     28     public static final String FAKE_REGION_VALUE = "__"; //$NON-NLS-1$
     29     public static final String NAME = "Region";
     30 
     31     private String mValue;
     32 
     33     /**
     34      * Creates and returns a qualifier from the given folder segment. If the segment is incorrect,
     35      * <code>null</code> is returned.
     36      * @param segment the folder segment from which to create a qualifier.
     37      * @return a new {@link RegionQualifier} object or <code>null</code>
     38      */
     39     public static RegionQualifier getQualifier(String segment) {
     40         Matcher m = sRegionPattern.matcher(segment);
     41         if (m.matches()) {
     42             RegionQualifier qualifier = new RegionQualifier();
     43             qualifier.mValue = m.group(1);
     44 
     45             return qualifier;
     46         }
     47         return null;
     48     }
     49 
     50     /**
     51      * Returns the folder name segment for the given value. This is equivalent to calling
     52      * {@link #toString()} on a {@link RegionQualifier} object.
     53      * @param value the value of the qualifier, as returned by {@link #getValue()}.
     54      */
     55     public static String getFolderSegment(String value) {
     56         if (value != null) {
     57             String segment = "r" + value.toUpperCase(); //$NON-NLS-1$
     58             if (sRegionPattern.matcher(segment).matches()) {
     59                 return segment;
     60             }
     61         }
     62 
     63         return "";  //$NON-NLS-1$
     64     }
     65 
     66     public RegionQualifier() {
     67 
     68     }
     69 
     70     public RegionQualifier(String value) {
     71         mValue = value;
     72     }
     73 
     74     public String getValue() {
     75         if (mValue != null) {
     76             return mValue;
     77         }
     78 
     79         return ""; //$NON-NLS-1$
     80     }
     81 
     82     @Override
     83     public String getName() {
     84         return NAME;
     85     }
     86 
     87     @Override
     88     public String getShortName() {
     89         return NAME;
     90     }
     91 
     92     @Override
     93     public boolean isValid() {
     94         return mValue != null;
     95     }
     96 
     97     @Override
     98     public boolean hasFakeValue() {
     99         return FAKE_REGION_VALUE.equals(mValue);
    100     }
    101 
    102     @Override
    103     public boolean checkAndSet(String value, FolderConfiguration config) {
    104         RegionQualifier qualifier = getQualifier(value);
    105         if (qualifier != null) {
    106             config.setRegionQualifier(qualifier);
    107             return true;
    108         }
    109 
    110         return false;
    111     }
    112 
    113     @Override
    114     public boolean equals(Object qualifier) {
    115         if (qualifier instanceof RegionQualifier) {
    116             if (mValue == null) {
    117                 return ((RegionQualifier)qualifier).mValue == null;
    118             }
    119             return mValue.equals(((RegionQualifier)qualifier).mValue);
    120         }
    121 
    122         return false;
    123     }
    124 
    125     @Override
    126     public int hashCode() {
    127         if (mValue != null) {
    128             return mValue.hashCode();
    129         }
    130 
    131         return 0;
    132     }
    133 
    134     /**
    135      * Returns the string used to represent this qualifier in the folder name.
    136      */
    137     @Override
    138     public String getFolderSegment() {
    139         return getFolderSegment(mValue);
    140     }
    141 
    142     @Override
    143     public String getShortDisplayValue() {
    144         if (mValue != null) {
    145             return mValue;
    146         }
    147 
    148         return ""; //$NON-NLS-1$
    149     }
    150 
    151     @Override
    152     public String getLongDisplayValue() {
    153         if (mValue != null) {
    154             return String.format("Region %s", mValue);
    155         }
    156 
    157         return ""; //$NON-NLS-1$
    158     }
    159 }
    160