AsteroidTest.java (3543B)
1 // Copyright (c) 2009, 2015 Tomas Hlavaty All rights reserved. 2 3 // Redistribution and use in source and binary forms, with or without 4 // modification, are permitted provided that the following conditions 5 // are met: 6 7 // Redistributions of source code must retain the above copyright 8 // notice, this list of conditions and the following 9 // disclaimer. Redistributions in binary form must reproduce the above 10 // copyright notice, this list of conditions and the following 11 // disclaimer in the documentation and/or other materials provided 12 // with the distribution. 13 14 // Neither the name of jmultimethod nor the names of its contributors 15 // may be used to endorse or promote products derived from this 16 // software without specific prior written permission. 17 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 21 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 22 // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 23 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 25 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 27 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 29 // OF THE POSSIBILITY OF SUCH DAMAGE. 30 31 // http://en.wikipedia.org/wiki/Multiple_dispatch 32 33 package jmultimethod; 34 35 public class AsteroidTest { 36 37 class Asteroid {} 38 39 class Spaceship {} 40 41 @Multi("collide") 42 public void collideOO(Object X, Object Y) { 43 log("?? Bang, what happened? ", X, Y); 44 } 45 46 @Multi("collide") 47 public void collideAA(Asteroid X, Asteroid Y) { 48 log("AA Look at the beautiful fireworks! ", X, Y); 49 } 50 51 @Multi("collide") 52 public void collideAS(Asteroid X, Spaceship Y) { 53 log("AS Is it fatal? ", X, Y); 54 } 55 56 @Multi("collide") 57 public void collideSA(Spaceship X, Asteroid Y) { 58 log("SA Is it fatal? ", X, Y); 59 } 60 61 @Multi("collide") 62 public void collideSS(Spaceship X, Spaceship Y) { 63 log("SS Who's fault was it? ", X, Y); 64 } 65 66 @Multi("collide") 67 public void collide1S(String X, Spaceship Y) { 68 log("1S any string? ", X, Y); 69 } 70 71 @Multi("collide") 72 public void collide2S(@V("hi") String X, Spaceship Y) { 73 log("2S 'hi' value? ", X, Y); 74 } 75 76 protected Multimethod mm = new Multimethod("collide", getClass()); 77 78 public void collide(Object X, Object Y) { 79 mm.invoke(this, X, Y); 80 } 81 82 public void run() { 83 Object A = new Asteroid(); 84 Object S = new Spaceship(); 85 collide(A, A); 86 collide(A, S); 87 collide(S, A); 88 collide(S, S); 89 collide(A, 1); 90 collide(2, A); 91 collide(S, 3); 92 collide(4, S); 93 collide(5, null); 94 collide(null, null); 95 collide("hi", S); 96 collide("hello", S); 97 } 98 99 public void log(Object... args) { 100 for(Object o: args) { 101 if(o instanceof String) { 102 System.out.print(" " + (String) o); 103 } else { 104 System.out.print(" " + o); 105 } 106 } 107 System.out.println(); 108 } 109 110 public static void main(String[] args) throws Exception { 111 AsteroidTest t = new AsteroidTest(); 112 t.run(); 113 } 114 }