/* * Copyright (C) 2009 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.sdklib; import com.android.sdklib.repository.PkgProps; import java.util.Properties; /** * Represents the version of a target or device. *
* A version is defined by an API level and an optional code name. *null instead.)true if the AndroidVersion is an API level equals to
* apiLevel.
*/
public boolean equals(int apiLevel) {
return mCodename == null && apiLevel == mApiLevel;
}
/**
* Compares the receiver with either an {@link AndroidVersion} object or a {@link String}
* object.
* If obj is a {@link String}, then the method will first check if it's a string
* representation of a number, in which case it'll compare it to the api level. Otherwise, it'll
* compare it against the code name.
* For all other type of object give as parameter, this method will return
* false.
*/
@Override
public boolean equals(Object obj) {
if (obj instanceof AndroidVersion) {
AndroidVersion version = (AndroidVersion)obj;
if (mCodename == null) {
return version.mCodename == null &&
mApiLevel == version.mApiLevel;
} else {
return mCodename.equals(version.mCodename) &&
mApiLevel == version.mApiLevel;
}
} else if (obj instanceof String) {
// if we have a code name, this must match.
if (mCodename != null) {
return mCodename.equals(obj);
}
// else we try to convert to a int and compare to the api level
try {
int value = Integer.parseInt((String)obj);
return value == mApiLevel;
} catch (NumberFormatException e) {
// not a number? we'll return false below.
}
}
return false;
}
@Override
public int hashCode() {
if (mCodename != null) {
return mCodename.hashCode();
}
// there may be some collisions between the hashcode of the codename and the api level
// but it's acceptable.
return mApiLevel;
}
/**
* Returns a string with the API Level and optional codename.
* Useful for debugging.
* For display purpose, please use {@link #getApiString()} instead.
*/
@Override
public String toString() {
String s = String.format("API %1$d", mApiLevel); //$NON-NLS-1$
if (isPreview()) {
s += String.format(", %1$s preview", mCodename); //$NON-NLS-1$
}
return s;
}
/**
* Compares this object with the specified object for order. Returns a
* negative integer, zero, or a positive integer as this object is less
* than, equal to, or greater than the specified object.
*
* @param o the Object to be compared.
* @return a negative integer, zero, or a positive integer as this object is
* less than, equal to, or greater than the specified object.
*/
@Override
public int compareTo(AndroidVersion o) {
return compareTo(o.mApiLevel, o.mCodename);
}
private int compareTo(int apiLevel, String codename) {
if (mCodename == null) {
if (codename == null) {
return mApiLevel - apiLevel;
} else {
if (mApiLevel == apiLevel) {
return -1; // same api level but argument is a preview for next version
}
return mApiLevel - apiLevel;
}
} else {
// 'this' is a preview
if (mApiLevel == apiLevel) {
if (codename == null) {
return +1;
} else {
return mCodename.compareTo(codename); // strange case where the 2 previews
// have different codename?
}
} else {
return mApiLevel - apiLevel;
}
}
}
/**
* Compares this version with the specified API and returns true if this version
* is greater or equal than the requested API -- that is the current version is a
* suitable min-api-level for the argument API.
*/
public boolean isGreaterOrEqualThan(int api) {
return compareTo(api, null /*codename*/) >= 0;
}
}