Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: 4ndre4 <626401fd-4bc7-4f8f-b641-563ae0f72920@626401fd-4bc7-4f8f-b641-563ae0f72920.invalid> Newsgroups: comp.lang.java.programmer Subject: Re: double == double Date: Sun, 16 Oct 2016 18:37:32 +0100 Organization: A noiseless patient Spider Lines: 99 Message-ID: References: <8737k0qd38.fsf@golem.phantasia.org> <87zim5es30.fsf@golem.phantasia.org> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Sun, 16 Oct 2016 17:37:16 -0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="917f255bfc34919e5a39afd079f17297"; logging-data="21475"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+RU8AEwuO6bTAbjG2Hjp4M" User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.4.0 In-Reply-To: Cancel-Lock: sha1:gQLKo2iL4ecDvwjA9GFyoyiViDE= Xref: csiph.com comp.lang.java.programmer:35788 On 16/10/2016 17:54, Arne Vajhøj wrote: [...] > But using String comparison does not work at all. I am not talking about string comparison. I am talking about specific comparison semantics that has to be implemented ad hoc. > Using a Version class can have the right semantics. But then it is > no longer a String that is the version. Okay, so your point was that using string comparison (not the type String in itself) is not a good idea for versions; in which case I agree. It sounded a bit different though. Using String to represent a version and implement the right semantics for comparison is totally sensible. > But I tend to like OO principles. And the above would be > about as non-OO'ish as something can be. Not at all. Sorry - maybe it's my limit - but I don't see anything wrong with the following - do you? List versions = new ArrayList<>(); versions.add(new Version("1.9.0-123")); versions.add(new Version("1.10.0-123")); versions.add(new Version("1.7.0-123")); versions.add(new Version("1.7.0-10")); versions.add(new Version("1.7.0-9")); Collections.sort(versions); for (Version version : versions) { System.out.println(version); } Output: 1.7.0-9 1.7.0-10 1.7.0-123 1.7.0-123 1.9.0-123 1.10.0-123 .... static class Version implements Comparable { private static Pattern PATTERN = Pattern.compile( "(\\d+)\\.(\\d+)\\.(\\d+)-(\\d+)"); private final ParsedVersion version; private final String versionAsString; class ParsedVersion { int major; int minor; int patch; int build; boolean lessThan(ParsedVersion other) { return major < other.major || minor < other.minor || patch < other.patch || build < other.build; } } Version(String versionAsString) throws InvalidVersionException { this.versionAsString = versionAsString; this.version = parse(); } public int compareTo(Version other) { if (versionAsString.equals(other.versionAsString)) return 0; if (version.lessThan(other.version)) return -1; return 1; } @Override public String toString() { return versionAsString; } private ParsedVersion parse() throws InvalidVersionException { Matcher matcher = PATTERN.matcher(versionAsString); if (!matcher.find()) { throw new InvalidVersionException(versionAsString); } ParsedVersion parsedVersion = new ParsedVersion(); parsedVersion.major = Integer.valueOf(matcher.group(1)); parsedVersion.minor = Integer.valueOf(matcher.group(2)); parsedVersion.patch = Integer.valueOf(matcher.group(3)); parsedVersion.build = Integer.valueOf(matcher.group(4)); return parsedVersion; } } -- 4ndre4