summaryrefslogtreecommitdiffstats
path: root/sched/src/com/android/sched/util/Version.java
blob: c1b7746affab6d24a2e7fdccc55f3ab7ed98ef66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
 * Copyright (C) 2015 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.sched.util;

import com.android.sched.util.findbugs.SuppressFBWarnings;
import com.android.sched.util.log.LoggerFactory;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.logging.Logger;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;

/**
 * A class describing version, release, build & code.
 */
public class Version {
  @Nonnull
  private static final String FILE_SUFFIX = "-version.properties";
  @Nonnull
  private static final Logger logger = LoggerFactory.getLogger();

  @Nonnull
  private String version;
  @Nonnull
  private String releaseName;
  private int    releaseCode;
  @Nonnull
  private SubReleaseKind subReleaseKind;
  private int    subReleaseCode;
  @CheckForNull
  private String buildId;
  @CheckForNull
  private String codeBase;

  // FINDBUGS Fields are initialized by init()
  @SuppressFBWarnings("NP_NONNULL_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR")
  public Version(@Nonnull String name, @Nonnull ClassLoader loader)
      throws IOException {
    String resourceName = name + FILE_SUFFIX;
    InputStream resourceStream = loader.getResourceAsStream(resourceName);
    if (resourceStream == null) {
      throw new FileNotFoundException(resourceName);
    }
    try {
      init(resourceStream);
    } finally {
      try {
        resourceStream.close();
      } catch (IOException e) {
        //
      }
    }
  }

  // FINDBUGS Fields are initialized by init()
  @SuppressFBWarnings("NP_NONNULL_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR")
  public Version(@Nonnull InputStream is) throws IOException {
    init(is);
  }

  public Version(int releaseCode, int subReleaseCode, @Nonnull SubReleaseKind subReleaseKind) {
    this.version = "Unknown";
    this.releaseName = "Unknown";
    this.releaseCode = releaseCode;
    this.subReleaseCode = subReleaseCode;
    this.subReleaseKind = subReleaseKind;
  }

  private void init(InputStream is) throws IOException {
    Properties prop = new Properties();
    prop.load(is);

    long versionFileVersion = Long.parseLong(prop.getProperty("version-file.version.code"));
    assert versionFileVersion >= 1;

    version = prop.getProperty("version");
    assert version != null;

    releaseName = prop.getProperty("version.release.name");
    assert releaseName != null;

    releaseCode = Integer.parseInt(prop.getProperty("version.release.code"));

    subReleaseCode = Integer.parseInt(prop.getProperty("version.sub-release.code"));

    subReleaseKind =
        SubReleaseKind.valueOf(SubReleaseKind.class, prop.getProperty("version.sub-release.kind"));

    buildId = prop.getProperty("version.buildid");
    if (buildId != null && buildId.isEmpty()) {
      buildId = null;
    }
    codeBase = prop.getProperty("version.sha");
    if (codeBase != null && codeBase.isEmpty()) {
      codeBase = null;
    }

    if (codeBase == null || buildId == null) {
      subReleaseKind = SubReleaseKind.ENGINEERING;
    }
  }

  @Nonnull
  public String getVersion() {
    return version;
  }

  @Nonnull
  public String getReleaseName() {
    return releaseName;
  }

  public int getReleaseCode() {
    return releaseCode;
  }

  @Nonnull
  public SubReleaseKind getSubReleaseKind() {
    return subReleaseKind;
  }

  public int getSubReleaseCode() {
    return subReleaseCode;
  }

  @CheckForNull
  public String getBuildId() {
    return buildId;
  }

  @CheckForNull
  public String getCodeBase() {
    return codeBase;
  }

  @Nonnull
  public String getVerboseVersion() {
    return version + " '" + releaseName + "' ("
                   + (buildId != null ? buildId : "engineering")
                   + (codeBase != null ? (' ' + codeBase) : "") + ")";
  }

  public boolean isOlderThan(@Nonnull Version other) throws UncomparableVersion {
    return compareTo(other) < 0;
  }

  public boolean isOlderOrEqualsThan(@Nonnull Version other) throws UncomparableVersion {
    return compareTo(other) <= 0;
  }

  public boolean isNewerThan(@Nonnull Version other) throws UncomparableVersion {
    return compareTo(other) > 0;
  }

  public boolean isNewerOrEqualsThan(@Nonnull Version other) throws UncomparableVersion {
    return compareTo(other) >= 0;
  }

  @Override
  public final boolean equals(Object obj) {
    if (obj == this) {
      return true;
    }
    if (obj instanceof Version) {
      Version other = (Version) obj;
      return version.equals(other.version)
          && releaseName.equals(other.releaseName)
          && releaseCode == other.releaseCode
          && subReleaseCode == other.subReleaseCode
          && subReleaseKind == other.subReleaseKind
          && ((buildId != null && buildId.equals(other.buildId))
              || (buildId == null && other.buildId == null))
          && ((codeBase != null && codeBase.equals(other.codeBase))
              || (codeBase == null && other.codeBase == null));
    }
    return false;
  }

  @Override
  public final int hashCode() {
    return version.hashCode() ^ releaseName.hashCode() ^ (releaseCode * 7) ^ (subReleaseCode * 17)
        ^ subReleaseKind.hashCode() ^ (buildId != null ? buildId.hashCode() : 0)
        ^ (codeBase != null ? codeBase.hashCode() : 0);
  }

  int compareTo(@Nonnull Version other) throws UncomparableVersion {
    if ((subReleaseKind == SubReleaseKind.ENGINEERING
        && other.getSubReleaseKind() != SubReleaseKind.ENGINEERING)
        || releaseCode < 0
        || subReleaseCode < 0
        || (subReleaseKind != SubReleaseKind.ENGINEERING
            && other.getSubReleaseKind() == SubReleaseKind.ENGINEERING)
        || other.getReleaseCode() < 0
        || other.getSubReleaseCode() < 0) {
      throw new UncomparableVersion(
          getVerboseVersion() + " is not comparable with " + other.getVerboseVersion());
    }

    if (this.releaseCode > other.getReleaseCode() || (
        this.releaseCode == other.getReleaseCode()
        && this.subReleaseCode > other.getSubReleaseCode())) {
      return 1;
    }


    if (this.releaseCode < other.getReleaseCode() || (
        this.releaseCode == other.getReleaseCode()
        && this.subReleaseCode < other.getSubReleaseCode())) {
      return -1;
    }

    return 0;
  }
}