View Javadoc
1   /*
2    * Licensed to The Apereo Foundation under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional
4    * information regarding copyright ownership.
5    *
6    *
7    * The Apereo Foundation licenses this file to you under the Educational
8    * Community License, Version 2.0 (the "License"); you may not use this file
9    * except in compliance with the License. You may obtain a copy of the License
10   * at:
11   *
12   *   http://opensource.org/licenses/ecl2.txt
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   *
20   */
21  package org.opencastproject.assetmanager.impl;
22  
23  import static org.opencastproject.util.EqualsUtil.hash;
24  
25  import org.opencastproject.assetmanager.api.Version;
26  
27  import javax.annotation.ParametersAreNonnullByDefault;
28  
29  /**
30   * The version of an archived media package or element.
31   * <p>
32   * This class encapsulates the actual data type used for identifying versions to minimize
33   * API changes.
34   */
35  @ParametersAreNonnullByDefault
36  public final class VersionImpl implements Version {
37    private static final long serialVersionUID = 3060347920702655628L;
38  
39    public static final VersionImpl FIRST = mk(0L);
40  
41    private final long nr;
42  
43    public VersionImpl(long nr) {
44      this.nr = nr;
45    }
46  
47    /** Constructor function. */
48    public static VersionImpl mk(long nr) {
49      return new VersionImpl(nr);
50    }
51  
52    public static VersionImpl mk(Version v) {
53      return new VersionImpl(Long.parseLong(v.toString()));
54    }
55  
56    /** Create the next version after the <code>latest</code>. */
57    public static VersionImpl next(long latest) {
58      return new VersionImpl(latest + 1);
59    }
60  
61    public long value() {
62      return nr;
63    }
64  
65    @Override
66    public boolean isOlder(Version v) {
67      return compareTo(v) < 0;
68    }
69  
70    @Override
71    public boolean isYounger(Version v) {
72      return compareTo(v) > 0;
73    }
74  
75    @Override
76    public int compareTo(Version v) {
77      return Long.compare(nr, RuntimeTypes.convert(v).nr);
78    }
79  
80    @Override
81    public boolean equals(Object that) {
82      return (this == that) || (that instanceof VersionImpl && eqFields((VersionImpl) that));
83    }
84  
85    private boolean eqFields(VersionImpl that) {
86      return this.nr == that.nr;
87    }
88  
89    @Override
90    public int hashCode() {
91      return hash(nr);
92    }
93  
94    @Override
95    public String toString() {
96      return String.valueOf(nr);
97    }
98  }