| 1 |
/* |
| 2 |
* Copyright (C) 2009 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 com.android.annotations.NonNull; |
| 20 |
import com.android.annotations.Nullable; |
| 21 |
|
| 22 |
import java.util.regex.Matcher; |
| 23 |
import java.util.regex.Pattern; |
| 24 |
|
| 25 |
/** |
| 26 |
* Resource Qualifier for Platform Version. |
| 27 |
*/ |
| 28 |
public final class VersionQualifier extends ResourceQualifier { |
| 29 |
/** Default version. This means the property is not set. */ |
| 30 |
private static final int DEFAULT_VERSION = -1; |
| 31 |
|
| 32 |
private static final Pattern sVersionPattern = Pattern.compile("^v(\\d+)$");//$NON-NLS-1$ |
| 33 |
|
| 34 |
private int mVersion = DEFAULT_VERSION; |
| 35 |
|
| 36 |
public static final String NAME = "Platform Version"; |
| 37 |
|
| 38 |
/** |
| 39 |
* Creates and returns a qualifier from the given folder segment. If the segment is incorrect, |
| 40 |
* <code>null</code> is returned. |
| 41 |
* @param segment the folder segment from which to create a qualifier. |
| 42 |
* @return a new {@link VersionQualifier} object or <code>null</code> |
| 43 |
*/ |
| 44 |
public static VersionQualifier getQualifier(String segment) { |
| 45 |
Matcher m = sVersionPattern.matcher(segment); |
| 46 |
if (m.matches()) { |
| 47 |
String v = m.group(1); |
| 48 |
|
| 49 |
int code; |
| 50 |
try { |
| 51 |
code = Integer.parseInt(v); |
| 52 |
} catch (NumberFormatException e) { |
| 53 |
// looks like the string we extracted wasn't a valid number. |
| 54 |
return null; |
| 55 |
} |
| 56 |
|
| 57 |
VersionQualifier qualifier = new VersionQualifier(); |
| 58 |
qualifier.mVersion = code; |
| 59 |
return qualifier; |
| 60 |
} |
| 61 |
|
| 62 |
return null; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Returns the folder name segment for the given value. This is equivalent to calling |
| 67 |
* {@link #toString()} on a {@link VersionQualifier} object. |
| 68 |
* @param version the value of the qualifier, as returned by {@link #getVersion()}. |
| 69 |
*/ |
| 70 |
public static String getFolderSegment(int version) { |
| 71 |
if (version != DEFAULT_VERSION) { |
| 72 |
return String.format("v%1$d", version); //$NON-NLS-1$ |
| 73 |
} |
| 74 |
|
| 75 |
return ""; //$NON-NLS-1$ |
| 76 |
} |
| 77 |
|
| 78 |
public VersionQualifier(int apiLevel) { |
| 79 |
mVersion = apiLevel; |
| 80 |
} |
| 81 |
|
| 82 |
public VersionQualifier() { |
| 83 |
//pass |
| 84 |
} |
| 85 |
|
| 86 |
public int getVersion() { |
| 87 |
return mVersion; |
| 88 |
} |
| 89 |
|
| 90 |
@Override |
| 91 |
public String getName() { |
| 92 |
return NAME; |
| 93 |
} |
| 94 |
|
| 95 |
@Override |
| 96 |
public String getShortName() { |
| 97 |
return "Version"; |
| 98 |
} |
| 99 |
|
| 100 |
@Override |
| 101 |
public int since() { |
| 102 |
return 1; |
| 103 |
} |
| 104 |
|
| 105 |
@Override |
| 106 |
public boolean isValid() { |
| 107 |
return mVersion != DEFAULT_VERSION; |
| 108 |
} |
| 109 |
|
| 110 |
@Override |
| 111 |
public boolean hasFakeValue() { |
| 112 |
return false; |
| 113 |
} |
| 114 |
|
| 115 |
@Override |
| 116 |
public boolean checkAndSet(String value, FolderConfiguration config) { |
| 117 |
VersionQualifier qualifier = getQualifier(value); |
| 118 |
if (qualifier != null) { |
| 119 |
config.setVersionQualifier(qualifier); |
| 120 |
return true; |
| 121 |
} |
| 122 |
|
| 123 |
return false; |
| 124 |
} |
| 125 |
|
| 126 |
@Override |
| 127 |
public boolean equals(Object qualifier) { |
| 128 |
return qualifier instanceof VersionQualifier |
| 129 |
&& mVersion == ((VersionQualifier) qualifier).mVersion; |
| 130 |
|
| 131 |
} |
| 132 |
|
| 133 |
@Override |
| 134 |
public boolean isMatchFor(ResourceQualifier qualifier) { |
| 135 |
if (qualifier instanceof VersionQualifier) { |
| 136 |
// it is considered a match if our api level is equal or lower to the given qualifier, |
| 137 |
// or the given qualifier doesn't specify an API Level. |
| 138 |
return mVersion <= ((VersionQualifier) qualifier).mVersion |
| 139 |
|| ((VersionQualifier)qualifier).mVersion == -1; |
| 140 |
} |
| 141 |
|
| 142 |
return false; |
| 143 |
} |
| 144 |
|
| 145 |
@Override |
| 146 |
public boolean isBetterMatchThan(@Nullable ResourceQualifier compareTo, |
| 147 |
@NonNull ResourceQualifier reference) { |
| 148 |
if (compareTo == null) { |
| 149 |
return true; |
| 150 |
} |
| 151 |
|
| 152 |
VersionQualifier compareQ = (VersionQualifier) compareTo; |
| 153 |
VersionQualifier referenceQ = (VersionQualifier) reference; |
| 154 |
|
| 155 |
if (compareQ.mVersion == referenceQ.mVersion) { |
| 156 |
// what we have is already the best possible match (exact match) |
| 157 |
return false; |
| 158 |
} else if (mVersion == referenceQ.mVersion) { |
| 159 |
// got new exact value, this is the best! |
| 160 |
return true; |
| 161 |
} else { |
| 162 |
// in all case we're going to prefer the higher version (since they have been filtered |
| 163 |
// to not be too high |
| 164 |
return mVersion > compareQ.mVersion; |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
@Override |
| 169 |
public int hashCode() { |
| 170 |
return mVersion; |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Returns the string used to represent this qualifier in the folder name. |
| 175 |
*/ |
| 176 |
@Override |
| 177 |
public String getFolderSegment() { |
| 178 |
return getFolderSegment(mVersion); |
| 179 |
} |
| 180 |
|
| 181 |
@Override |
| 182 |
public String getShortDisplayValue() { |
| 183 |
if (mVersion != DEFAULT_VERSION) { |
| 184 |
return String.format("API %1$d", mVersion); |
| 185 |
} |
| 186 |
|
| 187 |
return ""; //$NON-NLS-1$ |
| 188 |
} |
| 189 |
|
| 190 |
@Override |
| 191 |
public String getLongDisplayValue() { |
| 192 |
if (mVersion != DEFAULT_VERSION) { |
| 193 |
return String.format("API Level %1$d", mVersion); |
| 194 |
} |
| 195 |
|
| 196 |
return ""; //$NON-NLS-1$ |
| 197 |
} |
| 198 |
} |