| 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 |
|
| 20 |
import com.android.annotations.NonNull; |
| 21 |
import com.android.annotations.Nullable; |
| 22 |
|
| 23 |
/** |
| 24 |
* Base class for resource qualifiers. |
| 25 |
* |
| 26 |
* The resource qualifier classes are designed as immutable. |
| 27 |
*/ |
| 28 |
public abstract class ResourceQualifier implements Comparable<ResourceQualifier> { |
| 29 |
|
| 30 |
/** |
| 31 |
* Returns the human readable name of the qualifier. |
| 32 |
*/ |
| 33 |
public abstract String getName(); |
| 34 |
|
| 35 |
/** |
| 36 |
* Returns a shorter human readable name for the qualifier. |
| 37 |
* |
| 38 |
* @see #getName() |
| 39 |
*/ |
| 40 |
public abstract String getShortName(); |
| 41 |
|
| 42 |
/** |
| 43 |
* Returns the API level when this qualifier was added to Android. |
| 44 |
*/ |
| 45 |
public abstract int since(); |
| 46 |
|
| 47 |
/** |
| 48 |
* Whether this qualifier is deprecated. |
| 49 |
*/ |
| 50 |
public boolean deprecated() { |
| 51 |
return false; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Returns whether the qualifier has a valid filter value. |
| 56 |
*/ |
| 57 |
public abstract boolean isValid(); |
| 58 |
|
| 59 |
/** |
| 60 |
* Returns whether the qualifier has a fake value. |
| 61 |
* |
| 62 |
* Fake values are used internally and should not be used as real qualifier value. |
| 63 |
*/ |
| 64 |
public abstract boolean hasFakeValue(); |
| 65 |
|
| 66 |
/** |
| 67 |
* Check if the value is valid for this qualifier, and if so sets the value into a Folder |
| 68 |
* Configuration. |
| 69 |
* |
| 70 |
* @param value The value to check and set. Must not be null. |
| 71 |
* @param config The folder configuration to receive the value. Must not be null. |
| 72 |
* @return true if the value was valid and was set. |
| 73 |
*/ |
| 74 |
public abstract boolean checkAndSet(String value, FolderConfiguration config); |
| 75 |
|
| 76 |
/** |
| 77 |
* Returns a string formatted to be used in a folder name. |
| 78 |
*/ |
| 79 |
public abstract String getFolderSegment(); |
| 80 |
|
| 81 |
/** |
| 82 |
* Returns the qualifier that can be used in {@link #isBetterMatchThan(ResourceQualifier, |
| 83 |
* ResourceQualifier)} when no qualifier is present in the config. |
| 84 |
* |
| 85 |
* null has a special meaning when trying to match the best config (it's the worst qualifier, |
| 86 |
* unless not other alternative matches). If a qualifier type has a different definition of best |
| 87 |
* in regards to the null qualifier, this method should be subclassed and a special value should |
| 88 |
* be returned from here, which can then be used to call {@link #isBetterMatchThan |
| 89 |
* (ResourceQualifier, ResourceQualifier)} which can implement the custom logic. |
| 90 |
*/ |
| 91 |
public ResourceQualifier getNullQualifier() { |
| 92 |
return null; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Returns whether the given qualifier is a match for the receiver. |
| 97 |
* |
| 98 |
* The default implementation returns the result of {@link #equals(Object)}. |
| 99 |
* |
| 100 |
* Children class that re-implements this must implement {@link #isBetterMatchThan |
| 101 |
* (ResourceQualifier, ResourceQualifier)} too. |
| 102 |
* |
| 103 |
* @param qualifier the reference qualifier |
| 104 |
* @return true if the receiver is a match. |
| 105 |
*/ |
| 106 |
public boolean isMatchFor(ResourceQualifier qualifier) { |
| 107 |
return equals(qualifier); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Returns true if the receiver (this) is a better match for the given {@code reference} than |
| 112 |
* the given {@code compareTo} comparable. |
| 113 |
* |
| 114 |
* @param compareTo The {@link ResourceQualifier} to compare to. |
| 115 |
* @param reference The reference qualifier value for which the match is (from phone's |
| 116 |
* folderConfig). |
| 117 |
*/ |
| 118 |
public boolean isBetterMatchThan(@Nullable ResourceQualifier compareTo, |
| 119 |
@NonNull ResourceQualifier reference) { |
| 120 |
// the default is to always return false. This gives less overhead than always returning |
| 121 |
// true, as it would only compare same values anyway. |
| 122 |
return compareTo == null; |
| 123 |
} |
| 124 |
|
| 125 |
@Override |
| 126 |
public String toString() { |
| 127 |
return getFolderSegment(); |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Returns a string formatted for display purpose. |
| 132 |
*/ |
| 133 |
public abstract String getShortDisplayValue(); |
| 134 |
|
| 135 |
/** |
| 136 |
* Returns a string formatted for display purpose. |
| 137 |
*/ |
| 138 |
public abstract String getLongDisplayValue(); |
| 139 |
|
| 140 |
/** |
| 141 |
* Returns {@code true} if both objects are equal. |
| 142 |
* |
| 143 |
* This is declared as abstract to force children classes to implement it. |
| 144 |
*/ |
| 145 |
@Override |
| 146 |
public abstract boolean equals(Object object); |
| 147 |
|
| 148 |
/** |
| 149 |
* Returns a hash code value for the object. |
| 150 |
* |
| 151 |
* This is declared as abstract to force children classes to implement it. |
| 152 |
*/ |
| 153 |
@Override |
| 154 |
public abstract int hashCode(); |
| 155 |
|
| 156 |
@Override |
| 157 |
public final int compareTo(@NonNull ResourceQualifier o) { |
| 158 |
return toString().compareTo(o.toString()); |
| 159 |
} |
| 160 |
|
| 161 |
public static boolean isValid(@Nullable ResourceQualifier qualifier) { |
| 162 |
return qualifier != null && qualifier.isValid(); |
| 163 |
} |
| 164 |
} |