Path: csiph.com!aioe.org!.POSTED!not-for-mail From: =?UTF-8?Q?Arne_Vajh=c3=b8j?= Newsgroups: comp.lang.java.programmer Subject: Re: Comparison structure? Date: Tue, 4 Oct 2016 22:29:42 -0400 Organization: Aioe.org NNTP Server Lines: 66 Message-ID: References: <59c17fe5-61a6-46dc-918f-ee5aa348e3fd@googlegroups.com> NNTP-Posting-Host: PBuS014lhzVyHjLbXmo/ZQ.user.gioia.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit X-Complaints-To: abuse@aioe.org User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.3.0 X-Notice: Filtered by postfilter v. 0.8.2 Xref: csiph.com comp.lang.java.programmer:35722 On 10/4/2016 8:16 PM, Arne Vajhøj wrote: > Well - if one is competing in a code obfuscation contest then I will > suggest: > > int compareMe = 1; > final int assignMe = cases[Integer.signum(compareMe - compareTo) + > 1].option(compareMe, compareTo); > int compareTo = 2; > > with: > > @FunctionalInterface > private interface Case { > public int option(int compareMe, int compareTo); > } > private static Case[] cases = { (me,to) -> me, (me,to) -> 4, (me,to) > -> me + 2 }; > > :-) Well - I think the concept can actually be twisted into something that is almost readable! @FunctionalInterface private static interface Case { public int option(int compareMe, int compareTo); } public static abstract class Comparison { protected Case lt; protected Case eq; protected Case gt; private int compareMe; private int compareTo; public Comparison(int compareMe, int compareTo) { this.compareMe = compareMe; this.compareTo = compareTo; } public int execute() { switch(Integer.signum(compareMe - compareTo)) { case -1: return lt.option(compareMe, compareTo); case 0: return eq.option(compareMe, compareTo); case 1: return gt.option(compareMe, compareTo); default: throw new RuntimeException("Bye bye"); } } } int compareMe = 1; int compareTo = 2; final int assignMe = new Comparison(compareMe, compareTo) { { lt = (me,to) -> me; eq = (me,to) -> 4; gt = (me,to) -> me + 2; } }.execute(); Arne