// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.chromium.sdk;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * An object that describes the numeric part of version.
 */
public class Version implements Comparable<Version> {
  private final List<Integer> components;

  /**
   * Constructs an immutable Version instance given the numeric components of version.
   */
  public Version(Integer ... components) {
    this(Arrays.asList(components));
  }

  /**
   * Constructs an immutable Version instance given the numeric components of version.
   */
  public Version(List<Integer> components) {
    this.components = Collections.unmodifiableList(new ArrayList<Integer>(components));
  }

  /**
   * @return numeric components of version in form of list of integers
   */
  public List<Integer> getComponents() {
    return components;
  }

  @Override
  public boolean equals(Object obj) {
    if (obj == null || !(obj instanceof Version)) {
      return false;
    }
    Version that = (Version) obj;
    return this.components.equals(that.components);
  }

  @Override
  public int hashCode() {
    return components.hashCode();
  }

  public int compareTo(Version other) {
    for (int i = 0; i < this.components.size(); i++) {
      if (other.components.size() <= i) {
        // shorter version is less
        return +1;
      }
      int res = this.components.get(i).compareTo(other.components.get(i));
      if (res != 0) {
        return res;
      }
    }
    if (this.components.size() < other.components.size()) {
      return -1;
    } else {
      return 0;
    }
  }

  @Override
  public String toString() {
    return components.toString();
  }

  /**
   * Parses string as version as far as it is dot-delimited integer numbers.
   * @param text string representation of version; not null
   * @return new instance of version or null if text is not version.
   */
  public static Version parseString(String text) {
    int i = 0;
    List<Integer> components = new ArrayList<Integer>(4);
    while (i < text.length()) {
      int num = Character.digit(text.charAt(i), 10);
      if (num < 0) {
        break;
      }
      i++;
      while (i < text.length() && Character.digit(text.charAt(i), 10) >= 0) {
        num = num * 10 + Character.digit(text.charAt(i), 10);
        i++;
      }
      components.add(num);
      if (i < text.length() && text.charAt(i) == '.') {
        i++;
        continue;
      } else {
        break;
      }
    }
    if (components.isEmpty()) {
      return null;
    }
    return new Version(components);
  }
}

@@ -457,16 +457,54 @@
 ponents;
+%0A  private final String textComponent;
 %0A%0A  /**%0A
@@ -669,32 +669,37 @@
 (components)
+, null
 );%0A  %7D%0A
-%0A
   /**%0A   * C
@@ -769,32 +769,65 @@
 nents of version
+ and optional%0A   * text component
 .%0A   */%0A  public
@@ -851,32 +851,54 @@
 eger%3E components
+, String textComponent
 ) %7B%0A    this.com
@@ -973,21 +973,62 @@
 ents));%0A
+    this.textComponent = textComponent;%0A
   %7D%0A%0A
+%0A
   /**%0A  
@@ -1963,37 +1963,143 @@
     
-return components.toString();
+if (textComponent == null) %7B%0A      return components.toString();%0A    %7D else %7B%0A      return components.toString() + textComponent;%0A    %7D
 %0A  %7D
@@ -2893,32 +2893,154 @@
 ;%0A      %7D%0A    %7D%0A
+    String suffix;%0A    if (i %3C text.length()) %7B%0A      suffix = text.substring(i);%0A    %7D else %7B%0A      suffix = null;%0A    %7D%0A
     if (componen
@@ -3109,21 +3109,29 @@
 n(components
+, suffix
 );%0A  %7D%0A%7D%0A

Add text component to Version class