org.chromium.sdk/src/org/chromium/sdk/Version.java
changeset 2 e4420d2515f1
equal deleted inserted replaced
1:ef76fc2ac88c 2:e4420d2515f1
       
     1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
       
     2 // Use of this source code is governed by a BSD-style license that can be
       
     3 // found in the LICENSE file.
       
     4 
       
     5 package org.chromium.sdk;
       
     6 
       
     7 import java.util.ArrayList;
       
     8 import java.util.Arrays;
       
     9 import java.util.Collections;
       
    10 import java.util.List;
       
    11 
       
    12 /**
       
    13  * An object that describes the numeric part of version.
       
    14  */
       
    15 public class Version implements Comparable<Version> {
       
    16   private final List<Integer> components;
       
    17 
       
    18   /**
       
    19    * Constructs an immutable Version instance given the numeric components of version.
       
    20    */
       
    21   public Version(Integer ... components) {
       
    22     this(Arrays.asList(components));
       
    23   }
       
    24 
       
    25   /**
       
    26    * Constructs an immutable Version instance given the numeric components of version.
       
    27    */
       
    28   public Version(List<Integer> components) {
       
    29     this.components = Collections.unmodifiableList(new ArrayList<Integer>(components));
       
    30   }
       
    31 
       
    32   /**
       
    33    * @return numeric components of version in form of list of integers
       
    34    */
       
    35   public List<Integer> getComponents() {
       
    36     return components;
       
    37   }
       
    38 
       
    39   @Override
       
    40   public boolean equals(Object obj) {
       
    41     if (obj == null || !(obj instanceof Version)) {
       
    42       return false;
       
    43     }
       
    44     Version that = (Version) obj;
       
    45     return this.components.equals(that.components);
       
    46   }
       
    47 
       
    48   @Override
       
    49   public int hashCode() {
       
    50     return components.hashCode();
       
    51   }
       
    52 
       
    53   public int compareTo(Version other) {
       
    54     for (int i = 0; i < this.components.size(); i++) {
       
    55       if (other.components.size() <= i) {
       
    56         // shorter version is less
       
    57         return +1;
       
    58       }
       
    59       int res = this.components.get(i).compareTo(other.components.get(i));
       
    60       if (res != 0) {
       
    61         return res;
       
    62       }
       
    63     }
       
    64     if (this.components.size() < other.components.size()) {
       
    65       return -1;
       
    66     } else {
       
    67       return 0;
       
    68     }
       
    69   }
       
    70 
       
    71   @Override
       
    72   public String toString() {
       
    73     return components.toString();
       
    74   }
       
    75 
       
    76   /**
       
    77    * Parses string as version as far as it is dot-delimited integer numbers.
       
    78    * @param text string representation of version; not null
       
    79    * @return new instance of version or null if text is not version.
       
    80    */
       
    81   public static Version parseString(String text) {
       
    82     int i = 0;
       
    83     List<Integer> components = new ArrayList<Integer>(4);
       
    84     while (i < text.length()) {
       
    85       int num = Character.digit(text.charAt(i), 10);
       
    86       if (num < 0) {
       
    87         break;
       
    88       }
       
    89       i++;
       
    90       while (i < text.length() && Character.digit(text.charAt(i), 10) >= 0) {
       
    91         num = num * 10 + Character.digit(text.charAt(i), 10);
       
    92         i++;
       
    93       }
       
    94       components.add(num);
       
    95       if (i < text.length() && text.charAt(i) == '.') {
       
    96         i++;
       
    97         continue;
       
    98       } else {
       
    99         break;
       
   100       }
       
   101     }
       
   102     if (components.isEmpty()) {
       
   103       return null;
       
   104     }
       
   105     return new Version(components);
       
   106   }
       
   107 }