aoqi@0: /* aoqi@0: * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: /*SAM types: aoqi@0: 1. An interface that has a single abstract method aoqi@0: 2. Having more than one distinct methods, but only one is "real", the others are overriden public methods in Object - example: Comparator aoqi@0: 3. Having more than one methods due to inheritance, but they have the same signature aoqi@0: 4. Having more than one methods due to inheritance, but one of them has a subsignature of all other methods aoqi@0: a) parameter types compatible aoqi@0: b) return type substitutable aoqi@0: c) thrown type not conflicting with the thrown clause of any other method aoqi@0: d) mixed up aoqi@0: 5. Type-dependent SAM types aoqi@0: non-SAM types: aoqi@0: 6. An interface that has a single abstract method, which is also public method in Object aoqi@0: 7. Having more than one methods due to inheritance, and none of them has a subsignature of all other methods aoqi@0: */ aoqi@0: aoqi@0: import java.util.List; aoqi@0: import java.util.Collection; aoqi@0: import java.sql.SQLException; aoqi@0: import java.sql.SQLTransientException; aoqi@0: import java.util.concurrent.TimeoutException; aoqi@0: import java.io.*; aoqi@0: aoqi@0: interface A {int getOldest(List list);} aoqi@0: interface B {int getOldest(List list);} aoqi@0: interface C {int getOldest(List list);} aoqi@0: interface D {int getOldest(List list);} aoqi@0: interface E {int getOldest(Collection collection);} aoqi@0: //Not SAM type, case #7 aoqi@0: interface DE extends D, E {} aoqi@0: aoqi@0: interface Foo {int getAge(Number n);} aoqi@0: interface Bar {int getAge(Integer i);} aoqi@0: //Not SAM type, case #7 aoqi@0: interface FooBar extends Foo, Bar {} aoqi@0: aoqi@0: //Not SAM type, case #6 aoqi@0: interface Planet {boolean equals(Object o);} aoqi@0: aoqi@0: // SAM type interfaces: aoqi@0: // type #2: aoqi@0: //only one abstract non-Ojbect method getAge() aoqi@0: interface Mars extends Planet {int getAge(T t);} aoqi@0: //only one abstract non-Ojbect method increment() aoqi@0: interface Jupiter { aoqi@0: boolean equals(Object o); aoqi@0: String toString(); aoqi@0: int increment(int i); aoqi@0: } aoqi@0: aoqi@0: // type #3: aoqi@0: interface X {int getTotal(List arg);} aoqi@0: interface Y {int getTotal(List strs);} aoqi@0: //SAM type ([List], int, {}) aoqi@0: interface XY extends X, Y {} aoqi@0: //SAM type ([List], int, {}) aoqi@0: interface XYZ extends X, Y, XY {} aoqi@0: aoqi@0: // type #4 a): aoqi@0: //SAM type ([List], int, {}) aoqi@0: interface AB extends A, B {} aoqi@0: aoqi@0: // type #4 b): aoqi@0: interface F {Number getValue(String str);} aoqi@0: interface G {Integer getValue(String str);} aoqi@0: interface H {Serializable getValue(String str);} aoqi@0: interface I {Object getValue(String str);} aoqi@0: //SAM type ([String], Integer, {}) aoqi@0: interface FGHI extends F, G, H, I {} aoqi@0: aoqi@0: interface J {List getAll(String str);} aoqi@0: interface K {List getAll(String str);} aoqi@0: interface L {List getAll(String str);} aoqi@0: interface M {Collection getAll(String str);} aoqi@0: //SAM type ([String], List/List, {}) - the return type is flexible to some degree aoqi@0: interface JK extends J, K {} aoqi@0: //SAM type ([String], List/List, {}) aoqi@0: interface JL extends J, L {} aoqi@0: //SAM type ([String], List/List, {}) aoqi@0: interface JKL extends J, K, L {} aoqi@0: //SAM type ([String], List/List, {}) aoqi@0: interface JKLM extends J, K, L, M {} aoqi@0: aoqi@0: // type #4 c): aoqi@0: interface N {String getText(File f) throws IOException;} aoqi@0: interface O {String getText(File f) throws FileNotFoundException;} aoqi@0: interface P {String getText(File f) throws NullPointerException;} aoqi@0: //SAM type ([File], String, {FileNotFoundException}) aoqi@0: interface NO extends N, O {} aoqi@0: //SAM type ([File], String, {}) aoqi@0: interface NOP extends N, O, P {} aoqi@0: aoqi@0: interface Boo {int getAge(String s) throws IOException;} aoqi@0: interface Doo {int getAge(String s) throws SQLException;} aoqi@0: //SAM type ([String], int, {}) aoqi@0: interface BooDoo extends Boo, Doo {} aoqi@0: aoqi@0: // type #4 d): aoqi@0: interface Q {Iterable m(Iterable arg);} aoqi@0: interface R {Iterable m(Iterable arg);} aoqi@0: //SAM type ([Iterable], Iterable/Iterable, {}) aoqi@0: interface QR extends Q, R {} aoqi@0: aoqi@0: interface U {Collection foo(List arg) throws IOException, SQLTransientException;} aoqi@0: interface V {List foo(List arg) throws EOFException, SQLException, TimeoutException;} aoqi@0: interface W {List foo(List arg) throws Exception;} aoqi@0: //SAM type ([List], List/List, {EOFException, SQLTransientException}) aoqi@0: interface UV extends U, V {} aoqi@0: // SAM type ([List], List/List, {EOFException, SQLTransientException}) aoqi@0: interface UVW extends U, V, W {} aoqi@0: aoqi@0: // type #5: aoqi@0: // Not a SAM because sam-ness depends on instantiation of type-variables aoqi@0: interface Qoo {void m(T arg);} aoqi@0: interface Roo {void m(S arg);} aoqi@0: interface QooRoo extends Qoo, Roo {}