@Override public Object singleOrNull() throws TooManyElementsException { if(l.isEmpty()){ return null; } if(l.size() > 1) { throw new TooManyElementsException("There are " + l.size() + " scalar values, cannot return the single one"); } return l.get(0); }
@Override public Object single() throws EmptyElementsException, TooManyElementsException { if(l.isEmpty()) { throw new EmptyElementsException("No scalar values, cannot return the single one"); } if(l.size() > 1) { throw new TooManyElementsException("There are " + l.size() + " scalar values, cannot return the single one"); } return l.get(0); }
/** * */ public static Method getMethod(Class<?> cls,String name) throws ObjectNotFoundException, TooManyElementsException { List<Method> methods = getMethods(cls,name); if(methods.isEmpty()){ throw new ObjectNotFoundException("No such method '" + name + "' in class '" + cls.getName() + "'"); } if(methods.size() > 1){ throw new TooManyElementsException("There are " + methods.size() + " methods named '" + name + "' in class '" + cls.getName() + "'"); } return methods.get(0); }
@Override public E singleOrNull() throws TooManyElementsException { int size = size(); if(size == 0){ return null; } if(size > 1){ throw new TooManyElementsException(); } return get(0); }
@Override public E single() throws EmptyElementsException, TooManyElementsException { int size = size(); if(size == 0){ throw new EmptyElementsException(); } if(size > 1){ throw new TooManyElementsException(); } return get(0); }