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 int since() {
     98         return 1;
     99     }
    100 
    101     @Override
    102     public boolean isValid() {
    103         return mCode != DEFAULT_CODE;
    104     }
    105 
    106     @Override
    107     public boolean hasFakeValue() {
    108         return false;
    109     }
    110 
    111     @Override
    112     public boolean checkAndSet(String value, FolderConfiguration config) {
    113         CountryCodeQualifier qualifier = getQualifier(value);
    114         if (qualifier != null) {
    115             config.setCountryCodeQualifier(qualifier);
    116             return true;
    117         }
    118 
    119         return false;
    120     }
    121 
    122     @Override
    123     public boolean equals(Object qualifier) {
    124         if (qualifier instanceof CountryCodeQualifier) {
    125             return mCode == ((CountryCodeQualifier)qualifier).mCode;
    126         }
    127 
    128         return false;
    129     }
    130 
    131     @Override
    132     public int hashCode() {
    133         return mCode;
    134     }
    135 
    136     /**
    137      * Returns the string used to represent this qualifier in the folder name.
    138      */
    139     @Override
    140     public String getFolderSegment() {
    141         return getFolderSegment(mCode);
    142     }
    143 
    144     @Override
    145     public String getShortDisplayValue() {
    146         if (mCode != DEFAULT_CODE) {
    147             return String.format("MCC %1$d", mCode);
    148         }
    149 
    150         return ""; //$NON-NLS-1$
    151     }
    152 
    153     @Override
    154     public String getLongDisplayValue() {
    155         return getShortDisplayValue();
    156     }
    157 
    158 }
    159