8bba93f08cb5baa9a85bdfcefd34f252c013d54d
[pub/Android/ownCloud.git] / src / com / owncloud / android / utils / OwnCloudVersion.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 */
19
20 package com.owncloud.android.utils;
21
22 public class OwnCloudVersion implements Comparable<OwnCloudVersion> {
23 public static final OwnCloudVersion owncloud_v1 = new OwnCloudVersion(
24 0x010000);
25 public static final OwnCloudVersion owncloud_v2 = new OwnCloudVersion(
26 0x020000);
27 public static final OwnCloudVersion owncloud_v3 = new OwnCloudVersion(
28 0x030000);
29 public static final OwnCloudVersion owncloud_v4 = new OwnCloudVersion(
30 0x040000);
31 public static final OwnCloudVersion owncloud_v4_5 = new OwnCloudVersion(
32 0x040500);
33
34 // format is in version
35 // 0xAABBCC
36 // for version AA.BB.CC
37 // ie version 2.0.3 will be stored as 0x030003
38 private int mVersion;
39 private boolean mIsValid;
40
41 public OwnCloudVersion(int version) {
42 mVersion = version;
43 mIsValid = true;
44 }
45
46 public OwnCloudVersion(String version) {
47 mVersion = 0;
48 mIsValid = false;
49 parseVersionString(version);
50 }
51
52 public String toString() {
53 return ((mVersion >> 16) % 256) + "." + ((mVersion >> 8) % 256) + "."
54 + ((mVersion) % 256);
55 }
56
57 public boolean isVersionValid() {
58 return mIsValid;
59 }
60
61 @Override
62 public int compareTo(OwnCloudVersion another) {
63 return another.mVersion == mVersion ? 0
64 : another.mVersion < mVersion ? 1 : -1;
65 }
66
67 private void parseVersionString(String version) {
68 try {
69 String[] nums = version.split("\\.");
70 if (nums.length > 0) {
71 mVersion += Integer.parseInt(nums[0]);
72 }
73 mVersion = mVersion << 8;
74 if (nums.length > 1) {
75 mVersion += Integer.parseInt(nums[1]);
76 }
77 mVersion = mVersion << 8;
78 if (nums.length > 2) {
79 mVersion += Integer.parseInt(nums[2]);
80 }
81 mIsValid = true;
82 } catch (Exception e) {
83 mIsValid = false;
84 }
85 }
86 }