use relational operators in switch

前端 未结 7 2099
终归单人心
终归单人心 2020-12-11 06:55

Is there a way to use relational operators (<,<=,>,>=) in a switch statement?

int score = 95;

switch(score)  {
   case (score >= 90):
      // do s         


        
7条回答
  •  遥遥无期
    2020-12-11 07:36

    Obviously, this is not possible as a language construct. But, just for fun, we could implement it by ourselves!

    public class Switch {
    
        public static interface Action {
            V run();
        }
    
        private final T value;
        private boolean runAction = false;
        private boolean completed = false;
        private Action actionToRun;
    
        public Switch(T value) {
            this.value = value;
        }
    
        static public  Switch on(T value) {
            return new Switch(value);
        }
    
        public Switch ifTrue(boolean condition) {
            runAction |= condition;
            return this;
        }
    
        public Switch ifEquals(T other) {
            return ifTrue(value.equals(other));
        }
    
        public Switch byDefault(Action action) {
            this.actionToRun = action;
            return this;
        }
    
        public Switch then(Action action) {
            if (runAction && !completed) {
                actionToRun = action;
                completed = true;
            }
            return this;
        }
    
        public V getResult() {
            if (actionToRun == null) {
                throw new IllegalStateException("none of conditions matched and no default action was provided");
            }
            return actionToRun.run();
        }
    }
    

    Switch accepts any value to switch on and then provides functionality to match over boolean conditions (ifTrue method) or by exact matches (ifEquals method). Providing a value to switch on is needed just for the latter feature.

    After building the conditions, user invokes getResult to obtain the result.

    For example, we could create a method that tells us what it thinks about our score:

    String tellMeMyScore(int score) {
        return Switch. on(score).byDefault(new Action() {
            public String run() {
                return "really poor score";
            }
        }).ifTrue(score > 95).then(new Action() {
            public String run() {
                return "you rock!";
            }
        }).ifTrue(score > 65).then(new Action() {
            public String run() {
                return "not bad, not bad";
            }
        }).ifEquals(42).then(new Action() {
            public String run() {
                return "that's the answer!";
            }
        }).getResult();
    }
    

    This simple test:

    for (int score : new int[] { 97, 85, 66, 55, 42, 32, 4 }) {
        System.out.println(score + ": " + tellMeMyScore(score));
    }
    

    Prints out:

    97: you rock!
    85: not bad, not bad
    66: not bad, not bad
    55: really poor score
    42: that's the answer!
    32: really poor score
    4: really poor score
    

提交回复
热议问题