-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #249 from eclipse-passage/564328
Bug 564328 API revision | conditions | rethink key interfaces
- Loading branch information
Showing
13 changed files
with
494 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
...ssage.lic.base/src/org/eclipse/passage/lic/internal/base/version/BaseSemanticVersion.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2020 ArSysOp | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* https://www.eclipse.org/legal/epl-2.0/. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* ArSysOp - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.passage.lic.internal.base.version; | ||
|
||
import java.util.Objects; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
|
||
import org.eclipse.passage.lic.internal.api.version.SemanticVersion; | ||
|
||
@SuppressWarnings("restriction") | ||
public final class BaseSemanticVersion implements SemanticVersion { | ||
|
||
private final int major; | ||
private final int minor; | ||
private final int service; | ||
private final Optional<String> qualifier; | ||
|
||
public BaseSemanticVersion(int major, int minor, int service, Optional<String> qualifier) { | ||
Objects.requireNonNull(qualifier, "SemanticVersion::qualifier"); //$NON-NLS-1$ | ||
this.major = major; | ||
this.minor = minor; | ||
this.service = service; | ||
this.qualifier = qualifier; | ||
} | ||
|
||
public BaseSemanticVersion(int major, int minor, int service, String qualifier) { | ||
this(major, minor, service, Optional.ofNullable(qualifier)); | ||
} | ||
|
||
public BaseSemanticVersion(int major, int minor, int service) { | ||
this(major, minor, service, Optional.empty()); | ||
} | ||
|
||
@Override | ||
public int major() { | ||
return major; | ||
} | ||
|
||
@Override | ||
public int minor() { | ||
return minor; | ||
} | ||
|
||
@Override | ||
public int service() { | ||
return service; | ||
} | ||
|
||
@Override | ||
public boolean hasQualifier() { | ||
return qualifier.isPresent(); | ||
} | ||
|
||
@Override | ||
public String qualifier() { | ||
return qualifier.get(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
String numerical = new NumericalVersion(this).get().stream()// | ||
.map(i -> i.toString())// | ||
.collect(Collectors.joining(".")); //$NON-NLS-1$ | ||
return numerical + (hasQualifier() ? '.' + qualifier() : ""); //$NON-NLS-1$ | ||
} | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
...lipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/version/BaseVersion.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2020 ArSysOp | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* https://www.eclipse.org/legal/epl-2.0/. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* ArSysOp - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.passage.lic.internal.base.version; | ||
|
||
import java.util.Objects; | ||
|
||
import org.eclipse.passage.lic.internal.api.version.Version; | ||
|
||
/** | ||
* | ||
*/ | ||
@SuppressWarnings("restriction") | ||
public abstract class BaseVersion implements Version { | ||
|
||
@Override | ||
public final int hashCode() { | ||
return Objects.hash(value()); | ||
} | ||
|
||
@Override | ||
public final boolean equals(Object obj) { | ||
if (!Version.class.isInstance(obj)) { | ||
return false; | ||
} | ||
return value().equals(((Version) obj).value()); | ||
} | ||
|
||
@Override | ||
public final String toString() { | ||
return value(); | ||
} | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
...se.passage.lic.base/src/org/eclipse/passage/lic/internal/base/version/DefaultVersion.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2020 ArSysOp | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* https://www.eclipse.org/legal/epl-2.0/. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* ArSysOp - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.passage.lic.internal.base.version; | ||
|
||
import org.eclipse.passage.lic.internal.api.version.SemanticVersion; | ||
|
||
@SuppressWarnings("restriction") | ||
public final class DefaultVersion extends BaseVersion { | ||
|
||
@Override | ||
public String value() { | ||
return "0.0.0"; //$NON-NLS-1$ | ||
} | ||
|
||
@Override | ||
public SemanticVersion semantic() { | ||
return new BaseSemanticVersion(0, 0, 0); | ||
} | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
....passage.lic.base/src/org/eclipse/passage/lic/internal/base/version/NumericalVersion.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2020 ArSysOp | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* https://www.eclipse.org/legal/epl-2.0/. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* ArSysOp - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.passage.lic.internal.base.version; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.function.Supplier; | ||
|
||
import org.eclipse.passage.lic.internal.api.version.SemanticVersion; | ||
|
||
/** | ||
* Auxiliary unit operating over {@linkplain SemanticVersion} collects it's | ||
* numerical fields in a list. | ||
*/ | ||
@SuppressWarnings("restriction") | ||
public final class NumericalVersion implements Supplier<List<Integer>> { | ||
|
||
private final SemanticVersion version; | ||
|
||
public NumericalVersion(SemanticVersion version) { | ||
this.version = version; | ||
} | ||
|
||
@Override | ||
public List<Integer> get() { | ||
return Arrays.asList(version.major(), version.minor(), version.service()); | ||
} | ||
|
||
} |
81 changes: 81 additions & 0 deletions
81
...lipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/version/SafeVersion.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2020 ArSysOp | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* https://www.eclipse.org/legal/epl-2.0/. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* ArSysOp - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.passage.lic.internal.base.version; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
|
||
import org.eclipse.passage.lic.internal.api.version.SemanticVersion; | ||
|
||
/** | ||
* <p> | ||
* Builds a {@linkplain SemanticVersion} instance of the given {@code source}. | ||
* </p> | ||
* <p> | ||
* Provides the famous {@code default} version in case of any {@code source} | ||
* inconsistency. | ||
* </p> | ||
*/ | ||
@SuppressWarnings("restriction") | ||
public final class SafeVersion extends BaseVersion { | ||
|
||
private final Object source; | ||
private static String seprators = "\\."; //$NON-NLS-1$ | ||
|
||
public SafeVersion(Object source) { | ||
this.source = source; | ||
} | ||
|
||
@Override | ||
public String value() { | ||
return semantic().toString(); | ||
} | ||
|
||
@Override | ||
public SemanticVersion semantic() { | ||
String version = source.toString().trim(); | ||
List<String> segments = Arrays.stream(version.split(seprators))// | ||
.map(String::trim)// | ||
.collect(Collectors.toList()); | ||
Optional<String> qualifier = (segments.size() > 3) // | ||
? Optional.of(segments.get(3)) // | ||
: Optional.empty(); | ||
return build( // | ||
extractSegment(segments, 0), // major | ||
extractSegment(segments, 1), // minor | ||
extractSegment(segments, 2), // service | ||
qualifier); | ||
} | ||
|
||
private int extractSegment(List<String> split, int index) { | ||
if (split.size() > index) { | ||
try { | ||
return Integer.parseInt(split.get(index)); | ||
} catch (Exception e) { | ||
// ignore | ||
} | ||
} | ||
return 0; | ||
} | ||
|
||
private SemanticVersion build(int major, int minor, int service, Optional<String> qualifier) { | ||
return new BaseSemanticVersion(// | ||
major, // | ||
minor, // | ||
service, // | ||
qualifier.isPresent() && (major + minor + service > 0) ? qualifier : Optional.empty()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
....api.tests/src/org/eclipse/passage/lic/api/version/tests/SemanticVersionContractTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2020 ArSysOp | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* https://www.eclipse.org/legal/epl-2.0/. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* ArSysOp - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.passage.lic.api.version.tests; | ||
|
||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assume.assumeFalse; | ||
import static org.junit.Assume.assumeTrue; | ||
|
||
import org.eclipse.passage.lic.internal.api.version.SemanticVersion; | ||
import org.junit.Test; | ||
|
||
@SuppressWarnings("restriction") | ||
public abstract class SemanticVersionContractTest { | ||
|
||
@Test(expected = RuntimeException.class) | ||
public void notExistingQualifierRetrievalMustFail() { | ||
SemanticVersion version = withoutQualifier(); | ||
assumeFalse(version.hasQualifier()); | ||
version.qualifier(); | ||
} | ||
|
||
@Test | ||
public void existingQualifierMustPresent() { | ||
SemanticVersion version = withQualifier(); | ||
assumeTrue(version.hasQualifier()); | ||
assertNotNull(version.qualifier()); | ||
} | ||
|
||
protected abstract SemanticVersion withoutQualifier(); | ||
|
||
protected abstract SemanticVersion withQualifier(); | ||
} |
36 changes: 36 additions & 0 deletions
36
...sage.lic.api.tests/src/org/eclipse/passage/lic/api/version/tests/VersionContractTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2020 ArSysOp | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* https://www.eclipse.org/legal/epl-2.0/. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* ArSysOp - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.passage.lic.api.version.tests; | ||
|
||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import org.eclipse.passage.lic.internal.api.version.Version; | ||
import org.junit.Test; | ||
|
||
@SuppressWarnings("restriction") | ||
public abstract class VersionContractTest { | ||
|
||
@Test | ||
public void sameValuedVersionsAreEqual() { | ||
assertTrue(fromString("1.0.0").equals(fromString("1.0.0"))); //$NON-NLS-1$//$NON-NLS-2$ | ||
} | ||
|
||
@Test | ||
public void differentValuedVersionsAreNotEqual() { | ||
assertFalse(fromString("1.0.0").equals(fromString("1.0.1"))); //$NON-NLS-1$//$NON-NLS-2$ | ||
} | ||
|
||
protected abstract Version fromString(String source); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.