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