/** * Returns a version that matches the input service type and major version but * {@link #ANY} minor version. * * @param v input version type * @return equivalent version with any minor version */ public static Version anyMinorVersionOf(Version v) { return new Version(v.getServiceClass(), v.getMajor(), Version.ANY); }
/** * Adds an implied version (plus any of its nested dependencies) to the * implied versions list for this version. * @param v the implied version. */ private void addImpliedVersion(Version v) { if (!impliedVersions.contains(v)) { impliedVersions.add(v); for (Version impliedVersion : v.getImpliedVersions()) { addImpliedVersion(impliedVersion); } } }
/** * Returns {@code true} if this version is a later version than the argument, * on the basis of comparing the major and minor versions. For the purposes of * comparison, a value of {@link #ANY} is considered to be * {@link Integer#MAX_VALUE}, so no finite version number may come after it. * * @param v version to compare against * @return {@code true} if this version is later than the argument * @throws IllegalArgumentException if the provided version is not for the * same service as this version or for one implied by this version. */ public final boolean isAfter(Version v) { Version serviceVersion = findServiceVersion(impliedVersions, v.getServiceClass()); Preconditions.checkArgument(serviceVersion != null, "No relationship between versions"); int serviceMajor = raiseAny(serviceVersion.major); int vMajor = raiseAny(v.major); if (serviceMajor != vMajor) { return serviceMajor > vMajor; } else { return raiseAny(serviceVersion.minor) > raiseAny(v.minor); } }
/** * Compares two versions, where either may be null. */ static int compareVersion(Version a, Version b) { if (a == b) { return 0; } if (a == null) { return -1; } if (b == null) { return 1; } int compare = MetadataKey.compareClass( a.getServiceClass(), b.getServiceClass()); if (compare != 0) { return compare; } int aMajor = a.getMajor(); int bMajor = b.getMajor(); if (aMajor != bMajor) { return aMajor < bMajor ? -1 : 1; } int aMinor = a.getMinor(); int bMinor = b.getMinor(); return aMinor < bMinor ? -1 : (aMinor == bMinor ? 0 : 1); }
/** * Takes a list of {@link Version} instances and merges it into another * list. A version in the source list will overwrite any value for the * same service (if any) in the target list. * @param target the target list of versions to merge into. * @param source the source list of versions that will be merged. */ @VisibleForTesting static void mergeVersions(List<Version> target, List<Version> source) { // Check for conflicts with target list before making any changes, // accumulating the list of changed versions. for (Version checkVersion : source) { Version currentVersion = Version.findServiceVersion(target, checkVersion.getServiceClass()); if (currentVersion != null) { target.remove(currentVersion); } } // Add all of the new versions. target.addAll(source); }
@Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append(serviceClass.getName()); sb.append(':'); sb.append(getVersionString()); return sb.toString(); } }
/** * Sets the desired version for the current thread to the provided values. * This method will update any existing request version information set by * defaults or a previous call to this method. The specified version (and * any related implied versions} will be set for the current thread until the * {@link #resetThreadVersion()} method is called to reset to the version * information back to the default state. * * @param version the new active version for this request. */ public void setThreadVersion(Version version) { // Set the thread local to the list of versions implied by the requested // version. threadVersions.set( Collections.unmodifiableList(version.getImpliedVersions())); }
/** * Sets the service protocol version that will be used for requests associated * with this service. * * @param v new service protocol version. */ public void setProtocolVersion(Version v) { // Ensure that any set version is appropriate for this service type, based // upon the default type that was computed at construction time. if (!protocolVersion.getServiceClass().equals(v.getServiceClass())) { throw new IllegalArgumentException("Invalid service class, " + "was: " + v.getServiceClass() + ", expected: " + protocolVersion.getServiceClass()); } protocolVersion = v; }
return new Version(serviceClass, versionProperty); } catch (IllegalArgumentException iae) { throw new IllegalStateException(
/** * Creates a new Version instance for the specified service and defines * the major and minor versions for the service. * @param serviceClass the service type. * @param major the major revision number of the service. * @param minor the minor revision number of the service. * @throws NullPointerException if the service type is {@code null}. * @throws IllegalArgumentException if revision values are invalid. */ public Version(Class<? extends Service> serviceClass, int major, int minor, Version ... impliedVersions) throws NullPointerException, IllegalArgumentException { if (serviceClass == null) { throw new NullPointerException("Null service class"); } if (major < 0 && major != ANY) { throw new IllegalArgumentException("Invalid major version:" + major); } if (minor < 0 && minor != ANY) { throw new IllegalArgumentException("Invalid minor version:" + minor); } this.serviceClass = serviceClass; this.major = major; this.minor = minor; // Compute the full list of implied versions computeImpliedVersions(impliedVersions); }
/** * Compute the fully resolved list of implied versions, including the * local instance and all directly and indirectly implied versions. * @param versionList the list of directly implied versions. */ private void computeImpliedVersions(Version ... versionList) { impliedVersions.add(this); for (Version v : versionList) { addImpliedVersion(v); } }
List<Version> threadList = getThreadVersions(); if (threadList != null) { v = Version.findServiceVersion(threadList, serviceClass); v = Version.findServiceVersion(getDefaultVersions(), serviceClass); if (v == null) {
return false; } else if (!version.equals(other.version)) { return false;
/** * Compares two versions, where either may be null. */ static int compareVersion(Version a, Version b) { if (a == b) { return 0; } if (a == null) { return -1; } if (b == null) { return 1; } int compare = MetadataKey.compareClass( a.getServiceClass(), b.getServiceClass()); if (compare != 0) { return compare; } int aMajor = a.getMajor(); int bMajor = b.getMajor(); if (aMajor != bMajor) { return aMajor < bMajor ? -1 : 1; } int aMinor = a.getMinor(); int bMinor = b.getMinor(); return aMinor < bMinor ? -1 : (aMinor == bMinor ? 0 : 1); }
/** * Takes a list of {@link Version} instances and merges it into another * list. A version in the source list will overwrite any value for the * same service (if any) in the target list. * @param target the target list of versions to merge into. * @param source the source list of versions that will be merged. */ @VisibleForTesting static void mergeVersions(List<Version> target, List<Version> source) { // Check for conflicts with target list before making any changes, // accumulating the list of changed versions. for (Version checkVersion : source) { Version currentVersion = Version.findServiceVersion(target, checkVersion.getServiceClass()); if (currentVersion != null) { target.remove(currentVersion); } } // Add all of the new versions. target.addAll(source); }
@Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append(serviceClass.getName()); sb.append(':'); sb.append(getVersionString()); return sb.toString(); } }
/** * Sets the desired version for the current thread to the provided values. * This method will update any existing request version information set by * defaults or a previous call to this method. The specified version (and * any related implied versions} will be set for the current thread until the * {@link #resetThreadVersion()} method is called to reset to the version * information back to the default state. * * @param version the new active version for this request. */ public void setThreadVersion(Version version) { // Set the thread local to the list of versions implied by the requested // version. threadVersions.set( Collections.unmodifiableList(version.getImpliedVersions())); }
/** * Sets the service protocol version that will be used for requests associated * with this service. * * @param v new service protocol version. */ public void setProtocolVersion(Version v) { // Ensure that any set version is appropriate for this service type, based // upon the default type that was computed at construction time. if (!protocolVersion.getServiceClass().equals(v.getServiceClass())) { throw new IllegalArgumentException("Invalid service class, " + "was: " + v.getServiceClass() + ", expected: " + protocolVersion.getServiceClass()); } protocolVersion = v; }
return new Version(serviceClass, versionProperty); } catch (IllegalArgumentException iae) { throw new IllegalStateException(
/** * Creates a new Version instance for the specified service and defines * the major and minor versions for the service. * @param serviceClass the service type. * @param major the major revision number of the service. * @param minor the minor revision number of the service. * @throws NullPointerException if the service type is {@code null}. * @throws IllegalArgumentException if revision values are invalid. */ public Version(Class<? extends Service> serviceClass, int major, int minor, Version ... impliedVersions) throws NullPointerException, IllegalArgumentException { if (serviceClass == null) { throw new NullPointerException("Null service class"); } if (major < 0 && major != ANY) { throw new IllegalArgumentException("Invalid major version:" + major); } if (minor < 0 && minor != ANY) { throw new IllegalArgumentException("Invalid minor version:" + minor); } this.serviceClass = serviceClass; this.major = major; this.minor = minor; // Compute the full list of implied versions computeImpliedVersions(impliedVersions); }