Home | History | Annotate | Download | only in configuration
      1 /*
      2  * Copyright (C) 2008 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 Mobile Country Code.
     24  */
     25 public final class CountryCodeQualifier extends ResourceQualifier {
     26     /** Default pixel density value. This means the property is not set. */
     27     private final static int DEFAULT_CODE = -1;
     28 
     29     private final static Pattern sCountryCodePattern = Pattern.compile("^mcc(\\d{3})$");//$NON-NLS-1$
     30 
     31     private final int mCode;
     32 
     33     public static final String NAME = "Mobile Country Code";
     34 
     35     /**
     36      * Creates and returns a qualifier from the given folder segment. If the segment is incorrect,
     37      * <code>null</code> is returned.
     38      * @param segment the folder segment from which to create a qualifier.
     39      * @return a new {@link CountryCodeQualifier} object or <code>null</code>
     40      */
     41     public static CountryCodeQualifier getQualifier(String segment) {
     42         Matcher m = sCountryCodePattern.matcher(segment);
     43         if (m.matches()) {
     44             String v = m.group(1);
     45 
     46             int code = -1;
     47             try {
     48                 code = Integer.parseInt(v);
     49             } catch (NumberFormatException e) {
     50                 // looks like the string we extracted wasn't a valid number.
     51                 return null;
     52             }
     53 
     54             CountryCodeQualifier qualifier = new CountryCodeQualifier(code);
     55             return qualifier;
     56         }
     57 
     58         return null;
     59     }
     60 
     61     /**
     62      * Returns the folder name segment for the given value. This is equivalent to calling
     63      * {@link #toString()} on a {@link CountryCodeQualifier} object.
     64      * @param code the value of the qualifier, as returned by {@link #getCode()}.
     65      */
     66     public static String getFolderSegment(int code) {
     67         if (code != DEFAULT_CODE && code >= 100 && code <=999) { // code is 3 digit.) {
     68             return String.format("mcc%1$d", code); //$NON-NLS-1$
     69         }
     70 
     71         return ""; //$NON-NLS-1$
     72     }
     73 
     74     public CountryCodeQualifier() {
     75         this(DEFAULT_CODE);
     76     }
     77 
     78     public CountryCodeQualifier(int code) {
     79         mCode = code;
     80     }
     81 
     82     public int getCode() {
     83         return mCode;
     84     }
     85 
     86     @Override
     87     public String getName() {
     88         return NAME;
     89     }
     90 
     91     @Override
     92     public String getShortName() {
     93         return "Country Code";
     94     }
     95 
     96     @Override
     97     public boolean isValid() {
     98         return mCode != DEFAULT_CODE;
     99     }
    100 
    101     @Override
    102     public boolean hasFakeValue() {
    103         return false;
    104     }
    105 
    106     @Override
    107     public boolean checkAndSet(String value, FolderConfiguration config) {
    108         CountryCodeQualifier qualifier = getQualifier(value);
    109         if (qualifier != null) {
    110             config.setCountryCodeQualifier(qualifier);
    111             return true;
    112         }
    113 
    114         return false;
    115     }
    116 
    117     @Override
    118     public boolean equals(Object qualifier) {
    119         if (qualifier instanceof CountryCodeQualifier) {
    120             return mCode == ((CountryCodeQualifier)qualifier).mCode;
    121         }
    122 
    123         return false;
    124     }
    125 
    126     @Override
    127     public int hashCode() {
    128         return mCode;
    129     }
    130 
    131     /**
    132      * Returns the string used to represent this qualifier in the folder name.
    133      */
    134     @Override
    135     public String getFolderSegment() {
    136         return getFolderSegment(mCode);
    137     }
    138 
    139     @Override
    140     public String getShortDisplayValue() {
    141         if (mCode != DEFAULT_CODE) {
    142             return String.format("MCC %1$d", mCode);
    143         }
    144 
    145         return ""; //$NON-NLS-1$
    146     }
    147 
    148     @Override
    149     public String getLongDisplayValue() {
    150         return getShortDisplayValue();
    151     }
    152 
    153 }
    154