src_fm_fc_ms_ff
stringlengths
43
86.8k
target
stringlengths
20
276k
HttpConnection implements Connection { public Connection method(Method method) { req.method(method); return this; } private HttpConnection(); static Connection connect(String url); static Connection connect(URL url); Connection url(URL url); Connection url(String url); Connection proxy(Proxy proxy); Connection proxy(S...
@Test public void method() { Connection con = HttpConnection.connect("http: assertEquals(Connection.Method.GET, con.request().method()); con.method(Connection.Method.POST); assertEquals(Connection.Method.POST, con.request().method()); }
HttpConnection implements Connection { public Connection data(String key, String value) { req.data(KeyVal.create(key, value)); return this; } private HttpConnection(); static Connection connect(String url); static Connection connect(URL url); Connection url(URL url); Connection url(String url); Connection proxy(Proxy ...
@Test public void data() { Connection con = HttpConnection.connect("http: con.data("Name", "Val", "Foo", "bar"); Collection<Connection.KeyVal> values = con.request().data(); Object[] data = values.toArray(); Connection.KeyVal one = (Connection.KeyVal) data[0]; Connection.KeyVal two = (Connection.KeyVal) data[1]; assert...
IntegerListToStringFunction implements GetterFunction<List<Integer>, String> { @Nullable @Override public String apply(@Nullable List<Integer> input) { if (input == null) { return null; } if (input.size() == 0) { return ""; } StringBuilder builder = new StringBuilder(); builder.append(input.get(0)); for (int i = 1; i <...
@Test public void testApply() throws Exception { A a = new A(); Method m = A.class.getDeclaredMethod("getX"); GetterInvoker invoker = FunctionalGetterInvoker.create("x", m); a.setX(Lists.newArrayList(1, 2, 3)); assertThat((String) invoker.invoke(a), is("1,2,3")); a.setX(null); assertThat(invoker.invoke(a), nullValue())...
HttpConnection implements Connection { public Connection cookie(String name, String value) { req.cookie(name, value); return this; } private HttpConnection(); static Connection connect(String url); static Connection connect(URL url); Connection url(URL url); Connection url(String url); Connection proxy(Proxy proxy); C...
@Test public void cookie() { Connection con = HttpConnection.connect("http: con.cookie("Name", "Val"); assertEquals("Val", con.request().cookie("Name")); }
HttpConnection implements Connection { public Connection requestBody(String body) { req.requestBody(body); return this; } private HttpConnection(); static Connection connect(String url); static Connection connect(URL url); Connection url(URL url); Connection url(String url); Connection proxy(Proxy proxy); Connection p...
@Test public void requestBody() { Connection con = HttpConnection.connect("http: con.requestBody("foo"); assertEquals("foo", con.request().requestBody()); }
HttpConnection implements Connection { private static String encodeUrl(String url) { try { URL u = new URL(url); return encodeUrl(u).toExternalForm(); } catch (Exception e) { return url; } } private HttpConnection(); static Connection connect(String url); static Connection connect(URL url); Connection url(URL url); Co...
@Test public void encodeUrl() throws MalformedURLException { URL url1 = new URL("http: URL url2 = HttpConnection.encodeUrl(url1); assertEquals("http: }
StringUtil { public static String join(Collection strings, String sep) { return join(strings.iterator(), sep); } static String join(Collection strings, String sep); static String join(Iterator strings, String sep); static String padding(int width); static boolean isBlank(String string); static boolean isNumeric(String...
@Test public void join() { assertEquals("", StringUtil.join(Arrays.asList(""), " ")); assertEquals("one", StringUtil.join(Arrays.asList("one"), " ")); assertEquals("one two three", StringUtil.join(Arrays.asList("one", "two", "three"), " ")); }
StringUtil { public static String padding(int width) { if (width < 0) throw new IllegalArgumentException("width must be > 0"); if (width < padding.length) return padding[width]; char[] out = new char[width]; for (int i = 0; i < width; i++) out[i] = ' '; return String.valueOf(out); } static String join(Collection strin...
@Test public void padding() { assertEquals("", StringUtil.padding(0)); assertEquals(" ", StringUtil.padding(1)); assertEquals(" ", StringUtil.padding(2)); assertEquals(" ", StringUtil.padding(15)); assertEquals(" ", StringUtil.padding(45)); }
StringUtil { public static boolean isBlank(String string) { if (string == null || string.length() == 0) return true; int l = string.length(); for (int i = 0; i < l; i++) { if (!StringUtil.isWhitespace(string.codePointAt(i))) return false; } return true; } static String join(Collection strings, String sep); static Stri...
@Test public void isBlank() { assertTrue(StringUtil.isBlank(null)); assertTrue(StringUtil.isBlank("")); assertTrue(StringUtil.isBlank(" ")); assertTrue(StringUtil.isBlank(" \r\n ")); assertFalse(StringUtil.isBlank("hello")); assertFalse(StringUtil.isBlank(" hello ")); }
StringUtil { public static boolean isNumeric(String string) { if (string == null || string.length() == 0) return false; int l = string.length(); for (int i = 0; i < l; i++) { if (!Character.isDigit(string.codePointAt(i))) return false; } return true; } static String join(Collection strings, String sep); static String ...
@Test public void isNumeric() { assertFalse(StringUtil.isNumeric(null)); assertFalse(StringUtil.isNumeric(" ")); assertFalse(StringUtil.isNumeric("123 546")); assertFalse(StringUtil.isNumeric("hello")); assertFalse(StringUtil.isNumeric("123.334")); assertTrue(StringUtil.isNumeric("1")); assertTrue(StringUtil.isNumeric(...
StringUtil { public static boolean isWhitespace(int c){ return c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\r'; } static String join(Collection strings, String sep); static String join(Iterator strings, String sep); static String padding(int width); static boolean isBlank(String string); static boolean is...
@Test public void isWhitespace() { assertTrue(StringUtil.isWhitespace('\t')); assertTrue(StringUtil.isWhitespace('\n')); assertTrue(StringUtil.isWhitespace('\r')); assertTrue(StringUtil.isWhitespace('\f')); assertTrue(StringUtil.isWhitespace(' ')); assertFalse(StringUtil.isWhitespace('\u00a0')); assertFalse(StringUtil....
StringUtil { public static String normaliseWhitespace(String string) { StringBuilder sb = StringUtil.stringBuilder(); appendNormalisedWhitespace(sb, string, false); return sb.toString(); } static String join(Collection strings, String sep); static String join(Iterator strings, String sep); static String padding(int wi...
@Test public void normaliseWhiteSpace() { assertEquals(" ", normaliseWhitespace(" \r \n \r\n")); assertEquals(" hello there ", normaliseWhitespace(" hello \r \n there \n")); assertEquals("hello", normaliseWhitespace("hello")); assertEquals("hello there", normaliseWhitespace("hello\nthere")); } @Test public void normali...
IntegerToEnumFunction implements RuntimeSetterFunction<Integer, Enum> { @Nullable @Override public Enum apply(@Nullable Integer input, Type runtimeOutputType) { if (input == null) { return null; } Class<?> rawType = TypeToken.of(runtimeOutputType).getRawType(); EnumSet<?> es = cache.get(rawType); for (Enum<?> e : es) {...
@Test public void testApply() throws Exception { A a = new A(); Method m = A.class.getDeclaredMethod("setE", E.class); SetterInvoker invoker = FunctionalSetterInvoker.create("e", m); invoker.invoke(a, 2); assertThat(a.getE(), is(E.Z)); Method m2 = A.class.getDeclaredMethod("setE2", E2.class); SetterInvoker invoker2 = F...
StringUtil { public static URL resolve(URL base, String relUrl) throws MalformedURLException { if (relUrl.startsWith("?")) relUrl = base.getPath() + relUrl; if (relUrl.indexOf('.') == 0 && base.getFile().indexOf('/') != 0) { base = new URL(base.getProtocol(), base.getHost(), base.getPort(), "/" + base.getFile()); } ret...
@Test public void resolvesRelativeUrls() { assertEquals("http: assertEquals("http: assertEquals("http: assertEquals("https: assertEquals("http: assertEquals("https: assertEquals("https: assertEquals("https: assertEquals("https: assertEquals("https: assertEquals("", resolve("wrong", "also wrong")); assertEquals("ftp: as...
W3CDom { public Document fromJsoup(org.jsoup.nodes.Document in) { Validate.notNull(in); DocumentBuilder builder; try { factory.setNamespaceAware(true); builder = factory.newDocumentBuilder(); Document out = builder.newDocument(); convert(in, out); return out; } catch (ParserConfigurationException e) { throw new Illegal...
@Test public void namespacePreservation() throws IOException { File in = ParseTest.getFile("/htmltests/namespaces.xhtml"); org.jsoup.nodes.Document jsoupDoc; jsoupDoc = Jsoup.parse(in, "UTF-8"); Document doc; org.jsoup.helper.W3CDom jDom = new org.jsoup.helper.W3CDom(); doc = jDom.fromJsoup(jsoupDoc); Node htmlEl = doc...
DataUtil { static String getCharsetFromContentType(String contentType) { if (contentType == null) return null; Matcher m = charsetPattern.matcher(contentType); if (m.find()) { String charset = m.group(1).trim(); charset = charset.replace("charset=", ""); return validateCharset(charset); } return null; } private DataUt...
@Test public void testCharset() { assertEquals("utf-8", DataUtil.getCharsetFromContentType("text/html;charset=utf-8 ")); assertEquals("UTF-8", DataUtil.getCharsetFromContentType("text/html; charset=UTF-8")); assertEquals("ISO-8859-1", DataUtil.getCharsetFromContentType("text/html; charset=ISO-8859-1")); assertEquals(nu...
DataUtil { static Document parseInputStream(InputStream input, String charsetName, String baseUri, Parser parser) throws IOException { if (input == null) return new Document(baseUri); if (!(input instanceof ConstrainableInputStream)) input = new ConstrainableInputStream(input, bufferSize, 0); Document doc = null; boole...
@Test public void discardsSpuriousByteOrderMark() throws IOException { String html = "\uFEFF<html><head><title>One</title></head><body>Two</body></html>"; Document doc = DataUtil.parseInputStream(stream(html), "UTF-8", "http: assertEquals("One", doc.head().text()); } @Test public void discardsSpuriousByteOrderMarkWhenN...
StringToEnumFunction implements RuntimeSetterFunction<String, Enum> { @Nullable @Override public Enum apply(@Nullable String input, Type runtimeOutputType) { if (input == null) { return null; } Class rawType = TypeToken.of(runtimeOutputType).getRawType(); Enum r = Enum.valueOf(rawType, input); return r; } @Nullable @O...
@Test public void testApply() throws Exception { A a = new A(); Method m = A.class.getDeclaredMethod("setE", E.class); SetterInvoker invoker = FunctionalSetterInvoker.create("e", m); invoker.invoke(a, "Y"); assertThat(a.getE(), is(E.Y)); }
DataUtil { static String mimeBoundary() { final StringBuilder mime = new StringBuilder(boundaryLength); final Random rand = new Random(); for (int i = 0; i < boundaryLength; i++) { mime.append(mimeBoundaryChars[rand.nextInt(mimeBoundaryChars.length)]); } return mime.toString(); } private DataUtil(); static Document lo...
@Test public void generatesMimeBoundaries() { String m1 = DataUtil.mimeBoundary(); String m2 = DataUtil.mimeBoundary(); assertEquals(DataUtil.boundaryLength, m1.length()); assertEquals(DataUtil.boundaryLength, m2.length()); assertNotSame(m1, m2); }
ClosureUtils { public static <E> Closure<E> exceptionClosure() { return ExceptionClosure.<E>exceptionClosure(); } private ClosureUtils(); static Closure<E> exceptionClosure(); static Closure<E> nopClosure(); static Closure<E> asClosure(final Transformer<? super E, ?> transformer); static Closure<E> forClosure(final in...
@Test public void testExceptionClosure() { assertNotNull(ClosureUtils.exceptionClosure()); assertSame(ClosureUtils.exceptionClosure(), ClosureUtils.exceptionClosure()); try { ClosureUtils.exceptionClosure().execute(null); } catch (final FunctorException ex) { try { ClosureUtils.exceptionClosure().execute(cString); } ca...
ClosureUtils { public static <E> Closure<E> nopClosure() { return NOPClosure.<E>nopClosure(); } private ClosureUtils(); static Closure<E> exceptionClosure(); static Closure<E> nopClosure(); static Closure<E> asClosure(final Transformer<? super E, ?> transformer); static Closure<E> forClosure(final int count, final Clo...
@Test public void testNopClosure() { final StringBuilder buf = new StringBuilder("Hello"); ClosureUtils.nopClosure().execute(null); assertEquals("Hello", buf.toString()); ClosureUtils.nopClosure().execute("Hello"); assertEquals("Hello", buf.toString()); }
ClosureUtils { public static <E> Closure<E> invokerClosure(final String methodName) { return asClosure(InvokerTransformer.<E, Object>invokerTransformer(methodName)); } private ClosureUtils(); static Closure<E> exceptionClosure(); static Closure<E> nopClosure(); static Closure<E> asClosure(final Transformer<? super E, ...
@Test public void testInvokeClosure() { StringBuffer buf = new StringBuffer("Hello"); ClosureUtils.invokerClosure("reverse").execute(buf); assertEquals("olleH", buf.toString()); buf = new StringBuffer("Hello"); ClosureUtils.invokerClosure("setLength", new Class[] {Integer.TYPE}, new Object[] {Integer.valueOf(2)}).execu...
ClosureUtils { public static <E> Closure<E> forClosure(final int count, final Closure<? super E> closure) { return ForClosure.forClosure(count, closure); } private ClosureUtils(); static Closure<E> exceptionClosure(); static Closure<E> nopClosure(); static Closure<E> asClosure(final Transformer<? super E, ?> transform...
@Test public void testForClosure() { final MockClosure<Object> cmd = new MockClosure<Object>(); ClosureUtils.forClosure(5, cmd).execute(null); assertEquals(5, cmd.count); assertSame(NOPClosure.INSTANCE, ClosureUtils.forClosure(0, new MockClosure<Object>())); assertSame(NOPClosure.INSTANCE, ClosureUtils.forClosure(-1, n...
ClosureUtils { public static <E> Closure<E> whileClosure(final Predicate<? super E> predicate, final Closure<? super E> closure) { return WhileClosure.<E>whileClosure(predicate, closure, false); } private ClosureUtils(); static Closure<E> exceptionClosure(); static Closure<E> nopClosure(); static Closure<E> asClosure(...
@Test public void testWhileClosure() { MockClosure<Object> cmd = new MockClosure<Object>(); ClosureUtils.whileClosure(FalsePredicate.falsePredicate(), cmd).execute(null); assertEquals(0, cmd.count); cmd = new MockClosure<Object>(); ClosureUtils.whileClosure(PredicateUtils.uniquePredicate(), cmd).execute(null); assertEq...
EnumToStringFunction implements GetterFunction<Enum, String> { @Nullable @Override public String apply(@Nullable Enum input) { return input == null ? null : input.name(); } @Nullable @Override String apply(@Nullable Enum input); }
@Test public void testApply() throws Exception { A a = new A(); a.setE(E.Y); Method m = A.class.getDeclaredMethod("getE"); GetterInvoker invoker = FunctionalGetterInvoker.create("e", m); String r = (String) invoker.invoke(a); assertThat(r, is("Y")); }
ClosureUtils { public static <E> Closure<E> doWhileClosure(final Closure<? super E> closure, final Predicate<? super E> predicate) { return WhileClosure.<E>whileClosure(predicate, closure, true); } private ClosureUtils(); static Closure<E> exceptionClosure(); static Closure<E> nopClosure(); static Closure<E> asClosure...
@Test public void testDoWhileClosure() { MockClosure<Object> cmd = new MockClosure<Object>(); ClosureUtils.doWhileClosure(cmd, FalsePredicate.falsePredicate()).execute(null); assertEquals(1, cmd.count); cmd = new MockClosure<Object>(); ClosureUtils.doWhileClosure(cmd, PredicateUtils.uniquePredicate()).execute(null); as...
ClosureUtils { public static <E> Closure<E> chainedClosure(final Closure<? super E>... closures) { return ChainedClosure.chainedClosure(closures); } private ClosureUtils(); static Closure<E> exceptionClosure(); static Closure<E> nopClosure(); static Closure<E> asClosure(final Transformer<? super E, ?> transformer); st...
@Test @SuppressWarnings("unchecked") public void testChainedClosure() { MockClosure<Object> a = new MockClosure<Object>(); MockClosure<Object> b = new MockClosure<Object>(); ClosureUtils.chainedClosure(a, b).execute(null); assertEquals(1, a.count); assertEquals(1, b.count); a = new MockClosure<Object>(); b = new MockCl...
ClosureUtils { public static <E> Closure<E> ifClosure(final Predicate<? super E> predicate, final Closure<? super E> trueClosure) { return IfClosure.<E>ifClosure(predicate, trueClosure); } private ClosureUtils(); static Closure<E> exceptionClosure(); static Closure<E> nopClosure(); static Closure<E> asClosure(final Tr...
@Test public void testIfClosure() { MockClosure<Object> a = new MockClosure<Object>(); MockClosure<Object> b = null; ClosureUtils.ifClosure(TruePredicate.truePredicate(), a).execute(null); assertEquals(1, a.count); a = new MockClosure<Object>(); ClosureUtils.ifClosure(FalsePredicate.<Object>falsePredicate(), a).execute...
ClosureUtils { public static <E> Closure<E> switchClosure(final Predicate<? super E>[] predicates, final Closure<? super E>[] closures) { return SwitchClosure.<E>switchClosure(predicates, closures, null); } private ClosureUtils(); static Closure<E> exceptionClosure(); static Closure<E> nopClosure(); static Closure<E> ...
@Test @SuppressWarnings("unchecked") public void testSwitchClosure() { final MockClosure<String> a = new MockClosure<String>(); final MockClosure<String> b = new MockClosure<String>(); ClosureUtils.<String>switchClosure( new Predicate[] { EqualPredicate.equalPredicate("HELLO"), EqualPredicate.equalPredicate("THERE") },...
ClosureUtils { @SuppressWarnings("unchecked") public static <E> Closure<E> switchMapClosure(final Map<? extends E, Closure<E>> objectsAndClosures) { if (objectsAndClosures == null) { throw new NullPointerException("The object and closure map must not be null"); } final Closure<? super E> def = objectsAndClosures.remove...
@Test public void testSwitchMapClosure() { final MockClosure<String> a = new MockClosure<String>(); final MockClosure<String> b = new MockClosure<String>(); final Map<String, Closure<String>> map = new HashMap<String, Closure<String>>(); map.put("HELLO", a); map.put("THERE", b); ClosureUtils.switchMapClosure(map).execu...
ComparatorChain implements Comparator<E>, Serializable { @Override public int compare(final E o1, final E o2) throws UnsupportedOperationException { if (isLocked == false) { checkChainIntegrity(); isLocked = true; } final Iterator<Comparator<E>> comparators = comparatorChain.iterator(); for (int comparatorIndex = 0; co...
@Test public void testBadNoopComparatorChain() { final ComparatorChain<Integer> chain = new ComparatorChain<Integer>(); final Integer i1 = Integer.valueOf(4); final Integer i2 = Integer.valueOf(6); try { chain.compare(i1,i2); fail("An exception should be thrown when a chain contains zero comparators."); } catch (final ...
FixedOrderComparator implements Comparator<T>, Serializable { public boolean add(final T obj) { checkLocked(); final Integer position = map.put(obj, Integer.valueOf(counter++)); return position == null; } FixedOrderComparator(); FixedOrderComparator(final T... items); FixedOrderComparator(final List<T> items); boolea...
@Test public void testConstructorPlusAdd() { final FixedOrderComparator<String> comparator = new FixedOrderComparator<String>(); for (final String topCitie : topCities) { comparator.add(topCitie); } final String[] keys = topCities.clone(); assertComparatorYieldsOrder(keys, comparator); }
FixedOrderComparator implements Comparator<T>, Serializable { public boolean addAsEqual(final T existingObj, final T newObj) { checkLocked(); final Integer position = map.get(existingObj); if (position == null) { throw new IllegalArgumentException(existingObj + " not known to " + this); } final Integer result = map.put...
@Test public void testAddAsEqual() { final FixedOrderComparator<String> comparator = new FixedOrderComparator<String>(topCities); comparator.addAsEqual("New York", "Minneapolis"); assertEquals(0, comparator.compare("New York", "Minneapolis")); assertEquals(-1, comparator.compare("Tokyo", "Minneapolis")); assertEquals(1...
EnumToIntegerFunction implements GetterFunction<Enum, Integer> { @Nullable @Override public Integer apply(@Nullable Enum input) { return input == null ? null : input.ordinal(); } @Nullable @Override Integer apply(@Nullable Enum input); }
@Test public void testApply() throws Exception { A a = new A(); a.setE(E.Y); Method m = A.class.getDeclaredMethod("getE"); GetterInvoker invoker = FunctionalGetterInvoker.create("e", m); Integer r = (Integer) invoker.invoke(a); assertThat(r, is(1)); }
TypeToken extends TypeCapture<T> implements Serializable { public static <T> TypeToken<T> of(Class<T> type) { return new SimpleTypeToken<T>(type); } protected TypeToken(); private TypeToken(Type type); static TypeToken<T> of(Class<T> type); static TypeToken<?> of(Type type); final Class<? super T> getRawType(); final...
@Test public void testOf() throws Exception { TypeToken<String> token = TypeToken.of(String.class); assertThat(token.getType().equals(String.class), is(true)); assertThat(token.getRawType().equals(String.class), is(true)); }
SplitMapUtils { @SuppressWarnings("unchecked") public static <K, V> IterableMap<K, V> readableMap(final Get<K, V> get) { if (get == null) { throw new NullPointerException("Get must not be null"); } if (get instanceof Map) { return get instanceof IterableMap ? ((IterableMap<K, V>) get) : MapUtils.iterableMap((Map<K, V>)...
@Test public void testReadableMap() { final IterableMap<String, Integer> map = SplitMapUtils.readableMap(transformedMap); for (int i = 0; i < 10; i++) { assertFalse(map.containsValue(String.valueOf(i))); assertEquals(i, map.get(String.valueOf(i)).intValue()); } final MapIterator<String, Integer> it = map.mapIterator();...
SplitMapUtils { @SuppressWarnings("unchecked") public static <K, V> Map<K, V> writableMap(final Put<K, V> put) { if (put == null) { throw new NullPointerException("Put must not be null"); } if (put instanceof Map) { return (Map<K, V>) put; } return new WrappedPut<K, V>(put); } private SplitMapUtils(); @SuppressWarning...
@Test @SuppressWarnings("unchecked") public void testWritableMap() { final Map<String, String> map = SplitMapUtils.writableMap(transformedMap); attemptGetOperation(new Runnable() { public void run() { map.get(null); } }); attemptGetOperation(new Runnable() { public void run() { map.entrySet(); } }); attemptGetOperation...
BagUtils { public static <E> Bag<E> synchronizedBag(final Bag<E> bag) { return SynchronizedBag.synchronizedBag(bag); } private BagUtils(); static Bag<E> synchronizedBag(final Bag<E> bag); static Bag<E> unmodifiableBag(final Bag<? extends E> bag); static Bag<E> predicatedBag(final Bag<E> bag, final Predicate<? super E>...
@Test public void testSynchronizedBag() { Bag<Object> bag = BagUtils.synchronizedBag(new HashBag<Object>()); assertTrue("Returned object should be a SynchronizedBag.", bag instanceof SynchronizedBag); try { BagUtils.synchronizedBag(null); fail("Expecting NullPointerException for null bag."); } catch (final NullPointerE...
BagUtils { public static <E> Bag<E> unmodifiableBag(final Bag<? extends E> bag) { return UnmodifiableBag.unmodifiableBag(bag); } private BagUtils(); static Bag<E> synchronizedBag(final Bag<E> bag); static Bag<E> unmodifiableBag(final Bag<? extends E> bag); static Bag<E> predicatedBag(final Bag<E> bag, final Predicate<...
@Test public void testUnmodifiableBag() { Bag<Object> bag = BagUtils.unmodifiableBag(new HashBag<Object>()); assertTrue("Returned object should be an UnmodifiableBag.", bag instanceof UnmodifiableBag); try { BagUtils.unmodifiableBag(null); fail("Expecting NullPointerException for null bag."); } catch (final NullPointer...
BagUtils { public static <E> Bag<E> predicatedBag(final Bag<E> bag, final Predicate<? super E> predicate) { return PredicatedBag.predicatedBag(bag, predicate); } private BagUtils(); static Bag<E> synchronizedBag(final Bag<E> bag); static Bag<E> unmodifiableBag(final Bag<? extends E> bag); static Bag<E> predicatedBag(f...
@Test public void testPredicatedBag() { Bag<Object> bag = BagUtils.predicatedBag(new HashBag<Object>(), truePredicate); assertTrue("Returned object should be a PredicatedBag.", bag instanceof PredicatedBag); try { BagUtils.predicatedBag(null,truePredicate); fail("Expecting NullPointerException for null bag."); } catch ...
BagUtils { public static <E> Bag<E> transformingBag(final Bag<E> bag, final Transformer<? super E, ? extends E> transformer) { return TransformedBag.transformingBag(bag, transformer); } private BagUtils(); static Bag<E> synchronizedBag(final Bag<E> bag); static Bag<E> unmodifiableBag(final Bag<? extends E> bag); stati...
@Test public void testTransformedBag() { Bag<Object> bag = BagUtils.transformingBag(new HashBag<Object>(), nopTransformer); assertTrue("Returned object should be an TransformedBag.", bag instanceof TransformedBag); try { BagUtils.transformingBag(null, nopTransformer); fail("Expecting NullPointerException for null bag."...
BagUtils { public static <E> SortedBag<E> synchronizedSortedBag(final SortedBag<E> bag) { return SynchronizedSortedBag.synchronizedSortedBag(bag); } private BagUtils(); static Bag<E> synchronizedBag(final Bag<E> bag); static Bag<E> unmodifiableBag(final Bag<? extends E> bag); static Bag<E> predicatedBag(final Bag<E> b...
@Test public void testSynchronizedSortedBag() { Bag<Object> bag = BagUtils.synchronizedSortedBag(new TreeBag<Object>()); assertTrue("Returned object should be a SynchronizedSortedBag.", bag instanceof SynchronizedSortedBag); try { BagUtils.synchronizedSortedBag(null); fail("Expecting NullPointerException for null bag."...
BagUtils { public static <E> SortedBag<E> unmodifiableSortedBag(final SortedBag<E> bag) { return UnmodifiableSortedBag.unmodifiableSortedBag(bag); } private BagUtils(); static Bag<E> synchronizedBag(final Bag<E> bag); static Bag<E> unmodifiableBag(final Bag<? extends E> bag); static Bag<E> predicatedBag(final Bag<E> b...
@Test public void testUnmodifiableSortedBag() { SortedBag<Object> bag = BagUtils.unmodifiableSortedBag(new TreeBag<Object>()); assertTrue("Returned object should be an UnmodifiableSortedBag.", bag instanceof UnmodifiableSortedBag); try { BagUtils.unmodifiableSortedBag(null); fail("Expecting NullPointerException for nul...
Methods { static Type resolveType(Type type, TypeToken<?> daoTypeToken) { return genericTypeToken.isAssignableFrom(daoTypeToken) ? daoTypeToken.resolveType(type).getType() : type; } static MethodDescriptor getMethodDescriptor(Class<?> daoClass, Method method, boolean isUseActualParamName); static List<Method> listMeth...
@Test public void testResolveType() throws Exception { TypeToken<?> daoTypeToken = TypeToken.of(SubDao.class); Method m = SubDao.class.getMethod("add", Object.class); Type type = Methods.fixAndResolveType(m.getGenericReturnType(), daoTypeToken); assertThat(type, equalTo((Type) void.class)); type = Methods.fixAndResolve...
BagUtils { public static <E> SortedBag<E> predicatedSortedBag(final SortedBag<E> bag, final Predicate<? super E> predicate) { return PredicatedSortedBag.predicatedSortedBag(bag, predicate); } private BagUtils(); static Bag<E> synchronizedBag(final Bag<E> bag); static Bag<E> unmodifiableBag(final Bag<? extends E> bag);...
@Test public void testPredicatedSortedBag() { Bag<Object> bag = BagUtils.predicatedSortedBag(new TreeBag<Object>(), truePredicate); assertTrue("Returned object should be a PredicatedSortedBag.", bag instanceof PredicatedSortedBag); try { BagUtils.predicatedSortedBag(null, truePredicate); fail("Expecting NullPointerExce...
BagUtils { public static <E> SortedBag<E> transformingSortedBag(final SortedBag<E> bag, final Transformer<? super E, ? extends E> transformer) { return TransformedSortedBag.transformingSortedBag(bag, transformer); } private BagUtils(); static Bag<E> synchronizedBag(final Bag<E> bag); static Bag<E> unmodifiableBag(fina...
@Test public void testTransformedSortedBag() { Bag<Object> bag = BagUtils.transformingSortedBag(new TreeBag<Object>(), nopTransformer); assertTrue("Returned object should be an TransformedSortedBag", bag instanceof TransformedSortedBag); try { BagUtils.transformingSortedBag(null, nopTransformer); fail("Expecting NullPo...
IterableUtils { public static <E> void forEach(final Iterable<E> iterable, final Closure<? super E> closure) { IteratorUtils.forEach(emptyIteratorIfNull(iterable), closure); } @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Iterable<E> emptyIterable(); @SuppressWarnings("unche...
@Test public void forEach() { final List<Integer> listA = new ArrayList<Integer>(); listA.add(1); final List<Integer> listB = new ArrayList<Integer>(); listB.add(2); final Closure<List<Integer>> testClosure = ClosureUtils.invokerClosure("clear"); final Collection<List<Integer>> col = new ArrayList<List<Integer>>(); col...
IterableUtils { public static <E> E forEachButLast(final Iterable<E> iterable, final Closure<? super E> closure) { return IteratorUtils.forEachButLast(emptyIteratorIfNull(iterable), closure); } @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Iterable<E> emptyIterable(); @Suppr...
@Test public void forEachButLast() { final List<Integer> listA = new ArrayList<Integer>(); listA.add(1); final List<Integer> listB = new ArrayList<Integer>(); listB.add(2); final Closure<List<Integer>> testClosure = ClosureUtils.invokerClosure("clear"); final Collection<List<Integer>> col = new ArrayList<List<Integer>>...
IterableUtils { public static <E> boolean contains(final Iterable<E> iterable, final Object object) { if (iterable instanceof Collection<?>) { return ((Collection<E>) iterable).contains(object); } else { return IteratorUtils.contains(emptyIteratorIfNull(iterable), object); } } @SuppressWarnings("unchecked") // OK, emp...
@Test public void containsWithEquator() { final List<String> base = new ArrayList<String>(); base.add("AC"); base.add("BB"); base.add("CA"); final Equator<String> secondLetterEquator = new Equator<String>() { @Override public boolean equate(String o1, String o2) { return o1.charAt(1) == o2.charAt(1); } @Override public...
IterableUtils { public static <E, T extends E> int frequency(final Iterable<E> iterable, final T obj) { if (iterable instanceof Set<?>) { return ((Set<E>) iterable).contains(obj) ? 1 : 0; } if (iterable instanceof Bag<?>) { return ((Bag<E>) iterable).getCount(obj); } return size(filteredIterable(emptyIfNull(iterable), ...
@Test public void frequency() { assertEquals(0, IterableUtils.frequency(null, 1)); assertEquals(1, IterableUtils.frequency(iterableA, 1)); assertEquals(2, IterableUtils.frequency(iterableA, 2)); assertEquals(3, IterableUtils.frequency(iterableA, 3)); assertEquals(4, IterableUtils.frequency(iterableA, 4)); assertEquals(...
IterableUtils { public static <E> E find(final Iterable<E> iterable, final Predicate<? super E> predicate) { return IteratorUtils.find(emptyIteratorIfNull(iterable), predicate); } @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Iterable<E> emptyIterable(); @SuppressWarnings("u...
@Test public void find() { Predicate<Number> testPredicate = equalPredicate((Number) 4); Integer test = IterableUtils.find(iterableA, testPredicate); assertTrue(test.equals(4)); testPredicate = equalPredicate((Number) 45); test = IterableUtils.find(iterableA, testPredicate); assertTrue(test == null); assertNull(Iterabl...
IterableUtils { public static <E> int indexOf(final Iterable<E> iterable, final Predicate<? super E> predicate) { return IteratorUtils.indexOf(emptyIteratorIfNull(iterable), predicate); } @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Iterable<E> emptyIterable(); @SuppressWar...
@Test public void indexOf() { Predicate<Number> testPredicate = equalPredicate((Number) 4); int index = IterableUtils.indexOf(iterableA, testPredicate); assertEquals(6, index); testPredicate = equalPredicate((Number) 45); index = IterableUtils.indexOf(iterableA, testPredicate); assertEquals(-1, index); assertEquals(-1,...
InvocationInterceptorChain { public void intercept(BoundSql boundSql, InvocationContext context, DataSource dataSource) { if (interceptorChain.getInterceptors() != null) { List<Object> parameterValues = context.getParameterValues(); List<Parameter> parameters = new ArrayList<Parameter>(parameterValues.size()); for (int...
@Test public void testIntercept() throws Exception { final String sql = "select * from user where id=? and name=?"; BoundSql boundSql = new BoundSql(sql); boundSql.addArg(1); boundSql.addArg("ash"); final User user = new User(); user.setId(100); user.setName("lucy"); InterceptorChain ic = new InterceptorChain(); ic.add...
IterableUtils { public static <E> long countMatches(final Iterable<E> input, final Predicate<? super E> predicate) { if (predicate == null) { throw new NullPointerException("Predicate must not be null."); } return size(filteredIterable(emptyIfNull(input), predicate)); } @SuppressWarnings("unchecked") // OK, empty coll...
@Test public void countMatches() { assertEquals(4, IterableUtils.countMatches(iterableB, EQUALS_TWO)); assertEquals(0, IterableUtils.countMatches(null, EQUALS_TWO)); try { assertEquals(0, IterableUtils.countMatches(iterableA, null)); fail("predicate must not be null"); } catch (NullPointerException ex) { } try { assert...
IterableUtils { public static <E> boolean matchesAny(final Iterable<E> iterable, final Predicate<? super E> predicate) { return IteratorUtils.matchesAny(emptyIteratorIfNull(iterable), predicate); } @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Iterable<E> emptyIterable(); @S...
@Test public void matchesAny() { final List<Integer> list = new ArrayList<Integer>(); try { assertFalse(IterableUtils.matchesAny(null, null)); fail("predicate must not be null"); } catch (NullPointerException ex) { } try { assertFalse(IterableUtils.matchesAny(list, null)); fail("predicate must not be null"); } catch (N...
IterableUtils { public static <E> boolean matchesAll(final Iterable<E> iterable, final Predicate<? super E> predicate) { return IteratorUtils.matchesAll(emptyIteratorIfNull(iterable), predicate); } @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Iterable<E> emptyIterable(); @S...
@Test public void matchesAll() { try { assertFalse(IterableUtils.matchesAll(null, null)); fail("predicate must not be null"); } catch (NullPointerException ex) { } try { assertFalse(IterableUtils.matchesAll(iterableA, null)); fail("predicate must not be null"); } catch (NullPointerException ex) { } Predicate<Integer> l...
IterableUtils { public static <T> T get(final Iterable<T> iterable, final int index) { CollectionUtils.checkIndexBounds(index); if (iterable instanceof List<?>) { return ((List<T>) iterable).get(index); } return IteratorUtils.get(emptyIteratorIfNull(iterable), index); } @SuppressWarnings("unchecked") // OK, empty coll...
@Test(expected = IndexOutOfBoundsException.class) public void getFromIterable() throws Exception { final Bag<String> bag = new HashBag<String>(); bag.add("element", 1); assertEquals("element", IterableUtils.get(bag, 0)); IterableUtils.get(bag, 1); }
IterableUtils { public static <O> List<List<O>> partition(final Iterable<? extends O> iterable, final Predicate<? super O> predicate) { if (predicate == null) { throw new NullPointerException("Predicate must not be null."); } @SuppressWarnings({ "unchecked", "rawtypes" }) final Factory<List<O>> factory = FactoryUtils.i...
@SuppressWarnings("unchecked") @Test public void partition() { List<Integer> input = new ArrayList<Integer>(); input.add(1); input.add(2); input.add(3); input.add(4); List<List<Integer>> partitions = IterableUtils.partition(input, EQUALS_TWO); assertEquals(2, partitions.size()); Collection<Integer> partition = partitio...
IterableUtils { public static <E> String toString(final Iterable<E> iterable) { return IteratorUtils.toString(emptyIteratorIfNull(iterable)); } @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Iterable<E> emptyIterable(); @SuppressWarnings("unchecked") static Iterable<E> chaine...
@Test public void testToString() { String result = IterableUtils.toString(iterableA); assertEquals("[1, 2, 2, 3, 3, 3, 4, 4, 4, 4]", result); result = IterableUtils.toString(new ArrayList<Integer>()); assertEquals("[]", result); result = IterableUtils.toString(null); assertEquals("[]", result); result = IterableUtils.t...
SequencesComparator { public EditScript<T> getScript() { final EditScript<T> script = new EditScript<T>(); buildScript(0, sequence1.size(), 0, sequence2.size(), script); return script; } SequencesComparator(final List<T> sequence1, final List<T> sequence2); SequencesComparator(final List<T> sequence1, final List<T> se...
@Test public void testLength() { for (int i = 0; i < before.size(); ++i) { final SequencesComparator<Character> comparator = new SequencesComparator<Character>(sequence(before.get(i)), sequence(after.get(i))); Assert.assertEquals(length[i], comparator.getScript().getModifications()); } } @Test public void testExecution...
BatchUpdateOperator extends AbstractOperator { @Override public Object execute(Object[] values, InvocationStat stat) { Iterables iterables = getIterables(values); if (iterables.isEmpty()) { return transformer.transform(new int[]{}); } Map<DataSource, Group> gorupMap = new HashMap<DataSource, Group>(); int t = 0; for (O...
@Test public void testExecuteReturnVoid() throws Exception { TypeToken<List<User>> pt = new TypeToken<List<User>>() { }; TypeToken<Void> rt = TypeToken.of(void.class); String srcSql = "update user set name=:1.name where id=:1.id"; AbstractOperator operator = getOperator(pt, rt, srcSql); final int[] expectedInts = new i...
TrieUtils { public static <K, V> Trie<K, V> unmodifiableTrie(final Trie<K, ? extends V> trie) { return UnmodifiableTrie.unmodifiableTrie(trie); } private TrieUtils(); static Trie<K, V> unmodifiableTrie(final Trie<K, ? extends V> trie); }
@Test public void testUnmodifiableTrie() { Trie<String, Object> trie = TrieUtils.unmodifiableTrie(new PatriciaTrie<Object>()); assertTrue("Returned object should be an UnmodifiableTrie.", trie instanceof UnmodifiableTrie); try { TrieUtils.unmodifiableTrie(null); fail("Expecting NullPointerException for null trie."); } ...
QueueUtils { public static <E> Queue<E> unmodifiableQueue(final Queue<? extends E> queue) { return UnmodifiableQueue.unmodifiableQueue(queue); } private QueueUtils(); static Queue<E> unmodifiableQueue(final Queue<? extends E> queue); static Queue<E> predicatedQueue(final Queue<E> queue, final Predicate<? super E> pred...
@Test public void testUnmodifiableQueue() { Queue<Object> queue = QueueUtils.unmodifiableQueue(new LinkedList<Object>()); assertTrue("Returned object should be an UnmodifiableQueue.", queue instanceof UnmodifiableQueue); try { QueueUtils.unmodifiableQueue(null); fail("Expecting NullPointerException for null queue."); }...
QueueUtils { public static <E> Queue<E> predicatedQueue(final Queue<E> queue, final Predicate<? super E> predicate) { return PredicatedQueue.predicatedQueue(queue, predicate); } private QueueUtils(); static Queue<E> unmodifiableQueue(final Queue<? extends E> queue); static Queue<E> predicatedQueue(final Queue<E> queue...
@Test public void testPredicatedQueue() { Queue<Object> queue = QueueUtils.predicatedQueue(new LinkedList<Object>(), truePredicate); assertTrue("Returned object should be a PredicatedQueue.", queue instanceof PredicatedQueue); try { QueueUtils.predicatedQueue(null, truePredicate); fail("Expecting NullPointerException f...
QueueUtils { public static <E> Queue<E> transformingQueue(final Queue<E> queue, final Transformer<? super E, ? extends E> transformer) { return TransformedQueue.transformingQueue(queue, transformer); } private QueueUtils(); static Queue<E> unmodifiableQueue(final Queue<? extends E> queue); static Queue<E> predicatedQu...
@Test public void testTransformedQueue() { Queue<Object> queue = QueueUtils.transformingQueue(new LinkedList<Object>(), nopTransformer); assertTrue("Returned object should be an TransformedQueue.", queue instanceof TransformedQueue); try { QueueUtils.transformingQueue(null, nopTransformer); fail("Expecting NullPointerE...
QueueUtils { @SuppressWarnings("unchecked") public static <E> Queue<E> emptyQueue() { return (Queue<E>) EMPTY_QUEUE; } private QueueUtils(); static Queue<E> unmodifiableQueue(final Queue<? extends E> queue); static Queue<E> predicatedQueue(final Queue<E> queue, final Predicate<? super E> predicate); static Queue<E> tr...
@Test public void testEmptyQueue() { Queue<Object> queue = QueueUtils.emptyQueue(); assertTrue("Returned object should be an UnmodifiableQueue.", queue instanceof UnmodifiableQueue); assertTrue("Returned queue is not empty.", queue.isEmpty()); try { queue.add(new Object()); fail("Expecting UnsupportedOperationException...
EnumerationUtils { public static <E> List<E> toList(final Enumeration<? extends E> enumeration) { return IteratorUtils.toList(new EnumerationIterator<E>(enumeration)); } private EnumerationUtils(); static T get(final Enumeration<T> e, final int index); static List<E> toList(final Enumeration<? extends E> enumeration);...
@Test public void testToListWithStringTokenizer() { final List<String> expectedList1 = new ArrayList<String>(); final StringTokenizer st = new StringTokenizer(TO_LIST_FIXTURE); while (st.hasMoreTokens()) { expectedList1.add(st.nextToken()); } final List<String> expectedList2 = new ArrayList<String>(); expectedList2.add...
EnumerationUtils { public static <T> T get(final Enumeration<T> e, final int index) { int i = index; CollectionUtils.checkIndexBounds(i); while (e.hasMoreElements()) { i--; if (i == -1) { return e.nextElement(); } else { e.nextElement(); } } throw new IndexOutOfBoundsException("Entry does not exist: " + i); } private ...
@Test public void getFromEnumeration() throws Exception { final Vector<String> vector = new Vector<String>(); vector.addElement("zero"); vector.addElement("one"); Enumeration<String> en = vector.elements(); assertEquals("zero", EnumerationUtils.get(en, 0)); en = vector.elements(); assertEquals("one", EnumerationUtils.g...
BoundedIterator implements Iterator<E> { public void remove() { if (pos <= offset) { throw new IllegalStateException("remove() can not be called before calling next()"); } iterator.remove(); } BoundedIterator(final Iterator<? extends E> iterator, final long offset, final long max); boolean hasNext(); E next(); void rem...
@Test public void testRemoveWithoutCallingNext() { List<E> testListCopy = new ArrayList<E>(testList); Iterator<E> iter = new BoundedIterator<E>(testListCopy.iterator(), 1, 5); try { iter.remove(); fail("Expected IllegalStateException."); } catch (IllegalStateException ise) { } }
SkippingIterator extends AbstractIteratorDecorator<E> { @Override public E next() { final E next = super.next(); pos++; return next; } SkippingIterator(final Iterator<E> iterator, final long offset); @Override E next(); @Override void remove(); }
@Test public void testSkipping() { Iterator<E> iter = new SkippingIterator<E>(testList.iterator(), 2); assertTrue(iter.hasNext()); assertEquals("c", iter.next()); assertTrue(iter.hasNext()); assertEquals("d", iter.next()); assertTrue(iter.hasNext()); assertEquals("e", iter.next()); assertTrue(iter.hasNext()); assertEqu...
SkippingIterator extends AbstractIteratorDecorator<E> { @Override public void remove() { if (pos <= offset) { throw new IllegalStateException("remove() can not be called before calling next()"); } super.remove(); } SkippingIterator(final Iterator<E> iterator, final long offset); @Override E next(); @Override void remov...
@Test public void testRemoveWithoutCallingNext() { List<E> testListCopy = new ArrayList<E>(testList); Iterator<E> iter = new SkippingIterator<E>(testListCopy.iterator(), 1); try { iter.remove(); fail("Expected IllegalStateException."); } catch (IllegalStateException ise) { } }
PeekingIterator implements Iterator<E> { public boolean hasNext() { if (exhausted) { return false; } return slotFilled ? true : iterator.hasNext(); } PeekingIterator(final Iterator<? extends E> iterator); static PeekingIterator<E> peekingIterator(final Iterator<? extends E> iterator); boolean hasNext(); E peek(); E ele...
@Test public void testEmpty() { Iterator<E> it = makeEmptyIterator(); assertFalse(it.hasNext()); }
MapUtils { public static <K, V> IterableMap<K, V> predicatedMap(final Map<K, V> map, final Predicate<? super K> keyPred, final Predicate<? super V> valuePred) { return PredicatedMap.predicatedMap(map, keyPred, valuePred); } private MapUtils(); static V getObject(final Map<? super K, V> map, final K key); static String...
@Test public void testPredicatedMap() { final Predicate<Object> p = getPredicate(); Map<Object, Object> map = MapUtils.predicatedMap(new HashMap<Object, Object>(), p, p); assertTrue("returned object should be a PredicatedMap", map instanceof PredicatedMap); try { MapUtils.predicatedMap(null, p, p); fail("Expecting Null...
MapUtils { public static <K, V> IterableMap<K, V> lazyMap(final Map<K, V> map, final Factory<? extends V> factory) { return LazyMap.lazyMap(map, factory); } private MapUtils(); static V getObject(final Map<? super K, V> map, final K key); static String getString(final Map<? super K, ?> map, final K key); static Boolea...
@Test public void testLazyMapFactory() { final Factory<Integer> factory = FactoryUtils.constantFactory(Integer.valueOf(5)); Map<Object, Object> map = MapUtils.lazyMap(new HashMap<Object, Object>(), factory); assertTrue(map instanceof LazyMap); try { map = MapUtils.lazyMap(new HashMap<Object, Object>(), (Factory<Object>...
MapUtils { public static <K, V> Map<V, K> invertMap(final Map<K, V> map) { final Map<V, K> out = new HashMap<V, K>(map.size()); for (final Entry<K, V> entry : map.entrySet()) { out.put(entry.getValue(), entry.getKey()); } return out; } private MapUtils(); static V getObject(final Map<? super K, V> map, final K key); s...
@Test public void testInvertMap() { final Map<String, String> in = new HashMap<String, String>(5, 1); in.put("1", "A"); in.put("2", "B"); in.put("3", "C"); in.put("4", "D"); in.put("5", "E"); final Set<String> inKeySet = new HashSet<String>(in.keySet()); final Set<String> inValSet = new HashSet<String>(in.values()); fi...
MapUtils { @SuppressWarnings("unchecked") public static <K, V> Map<K, V> putAll(final Map<K, V> map, final Object[] array) { if (map == null) { throw new NullPointerException("The map must not be null"); } if (array == null || array.length == 0) { return map; } final Object obj = array[0]; if (obj instanceof Map.Entry)...
@Test public void testPutAll_Map_array() { try { MapUtils.putAll(null, null); fail(); } catch (final NullPointerException ex) {} try { MapUtils.putAll(null, new Object[0]); fail(); } catch (final NullPointerException ex) {} Map<String, String> test = MapUtils.putAll(new HashMap<String, String>(), new String[0]); assert...
MapUtils { public static Map<String, Object> toMap(final ResourceBundle resourceBundle) { final Enumeration<String> enumeration = resourceBundle.getKeys(); final Map<String, Object> map = new HashMap<String, Object>(); while (enumeration.hasMoreElements()) { final String key = enumeration.nextElement(); final Object va...
@Test public void testConvertResourceBundle() { final Map<String, String> in = new HashMap<String, String>( 5 , 1 ); in.put("1", "A"); in.put("2", "B"); in.put("3", "C"); in.put("4", "D"); in.put("5", "E"); final ResourceBundle b = new ListResourceBundle() { @Override public Object[][] getContents() { final Object[][] ...
MapUtils { public static void debugPrint(final PrintStream out, final Object label, final Map<?, ?> map) { verbosePrintInternal(out, label, map, new ArrayDeque<Map<?, ?>>(), true); } private MapUtils(); static V getObject(final Map<? super K, V> map, final K key); static String getString(final Map<? super K, ?> map, f...
@Test public void testDebugAndVerbosePrintCasting() { final Map<Integer, String> inner = new HashMap<Integer, String>(2, 1); inner.put(2, "B"); inner.put(3, "C"); final Map<Integer, Object> outer = new HashMap<Integer, Object>(2, 1); outer.put(0, inner); outer.put(1, "A"); final ByteArrayOutputStream out = new ByteArra...
MapUtils { public static void verbosePrint(final PrintStream out, final Object label, final Map<?, ?> map) { verbosePrintInternal(out, label, map, new ArrayDeque<Map<?, ?>>(), false); } private MapUtils(); static V getObject(final Map<? super K, V> map, final K key); static String getString(final Map<? super K, ?> map...
@Test public void testVerbosePrintNullLabel() { final ByteArrayOutputStream out = new ByteArrayOutputStream(); final PrintStream outPrint = new PrintStream(out); final String INDENT = " "; final Map<Integer, String> map = new TreeMap<Integer, String>(); map.put(2, "B"); map.put(3, "C"); map.put(4, null); outPrint.print...
MapUtils { public static <K,V> Map<K,V> emptyIfNull(final Map<K,V> map) { return map == null ? Collections.<K,V>emptyMap() : map; } private MapUtils(); static V getObject(final Map<? super K, V> map, final K key); static String getString(final Map<? super K, ?> map, final K key); static Boolean getBoolean(final Map<? ...
@Test public void testEmptyIfNull() { assertTrue(MapUtils.emptyIfNull(null).isEmpty()); final Map<Long, Long> map = new HashMap<Long, Long>(); assertSame(map, MapUtils.emptyIfNull(map)); }
MapUtils { public static boolean isEmpty(final Map<?,?> map) { return map == null || map.isEmpty(); } private MapUtils(); static V getObject(final Map<? super K, V> map, final K key); static String getString(final Map<? super K, ?> map, final K key); static Boolean getBoolean(final Map<? super K, ?> map, final K key);...
@Test public void testIsEmptyWithEmptyMap() { final Map<Object, Object> map = new HashMap<Object, Object>(); assertEquals(true, MapUtils.isEmpty(map)); } @Test public void testIsEmptyWithNonEmptyMap() { final Map<String, String> map = new HashMap<String, String>(); map.put("item", "value"); assertEquals(false, MapUtils...
MapUtils { public static boolean isNotEmpty(final Map<?,?> map) { return !MapUtils.isEmpty(map); } private MapUtils(); static V getObject(final Map<? super K, V> map, final K key); static String getString(final Map<? super K, ?> map, final K key); static Boolean getBoolean(final Map<? super K, ?> map, final K key); st...
@Test public void testIsNotEmptyWithEmptyMap() { final Map<Object, Object> map = new HashMap<Object, Object>(); assertEquals(false, MapUtils.isNotEmpty(map)); } @Test public void testIsNotEmptyWithNonEmptyMap() { final Map<String, String> map = new HashMap<String, String>(); map.put("item", "value"); assertEquals(true,...
MapUtils { public static <K, V> void populateMap(final Map<K, V> map, final Iterable<? extends V> elements, final Transformer<V, K> keyTransformer) { populateMap(map, elements, keyTransformer, TransformerUtils.<V>nopTransformer()); } private MapUtils(); static V getObject(final Map<? super K, V> map, final K key); sta...
@Test public void testPopulateMap() { final List<String> list = new ArrayList<String>(); list.add("1"); list.add("3"); list.add("5"); list.add("7"); list.add("2"); list.add("4"); list.add("6"); Map<Object, Object> map = new HashMap<Object, Object>(); MapUtils.populateMap(map, list, TransformedCollectionTest.STRING_TO_I...
MapUtils { public static <K, V> IterableMap<K, V> iterableMap(final Map<K, V> map) { if (map == null) { throw new NullPointerException("Map must not be null"); } return map instanceof IterableMap ? (IterableMap<K, V>) map : new AbstractMapDecorator<K, V>(map) {}; } private MapUtils(); static V getObject(final Map<? su...
@Test public void testIterableMap() { try { MapUtils.iterableMap(null); fail("Should throw NullPointerException"); } catch (final NullPointerException e) { } final HashMap<String, String> map = new HashMap<String, String>(); map.put("foo", "foov"); map.put("bar", "barv"); map.put("baz", "bazv"); final IterableMap<Strin...
MapUtils { public static <K, V> IterableSortedMap<K, V> iterableSortedMap(final SortedMap<K, V> sortedMap) { if (sortedMap == null) { throw new NullPointerException("Map must not be null"); } return sortedMap instanceof IterableSortedMap ? (IterableSortedMap<K, V>) sortedMap : new AbstractSortedMapDecorator<K, V>(sorte...
@Test public void testIterableSortedMap() { try { MapUtils.iterableSortedMap(null); fail("Should throw NullPointerException"); } catch (final NullPointerException e) { } final TreeMap<String, String> map = new TreeMap<String, String>(); map.put("foo", "foov"); map.put("bar", "barv"); map.put("baz", "bazv"); final Itera...
ListUtils { public static <E> List<E> intersection(final List<? extends E> list1, final List<? extends E> list2) { final List<E> result = new ArrayList<E>(); List<? extends E> smaller = list1; List<? extends E> larger = list2; if (list1.size() > list2.size()) { smaller = list2; larger = list1; } final HashSet<E> hashSe...
@Test public void testIntersectNonEmptyWithEmptyList() { final List<String> empty = Collections.<String>emptyList(); assertTrue("result not empty", ListUtils.intersection(empty, fullList).isEmpty()); } @Test public void testIntersectEmptyWithEmptyList() { final List<?> empty = Collections.EMPTY_LIST; assertTrue("result...
ListUtils { public static <E> List<E> predicatedList(final List<E> list, final Predicate<E> predicate) { return PredicatedList.predicatedList(list, predicate); } private ListUtils(); static List<T> emptyIfNull(final List<T> list); static List<T> defaultIfNull(final List<T> list, final List<T> defaultList); static List...
@Test public void testPredicatedList() { final Predicate<Object> predicate = new Predicate<Object>() { public boolean evaluate(final Object o) { return o instanceof String; } }; List<Object> list = ListUtils.predicatedList(new ArrayList<Object>(), predicate); assertTrue("returned object should be a PredicatedList", lis...
ListUtils { public static <E> List<E> lazyList(final List<E> list, final Factory<? extends E> factory) { return LazyList.lazyList(list, factory); } private ListUtils(); static List<T> emptyIfNull(final List<T> list); static List<T> defaultIfNull(final List<T> list, final List<T> defaultList); static List<E> intersecti...
@Test public void testLazyList() { final List<Integer> list = ListUtils.lazyList(new ArrayList<Integer>(), new Factory<Integer>() { private int index; public Integer create() { index++; return Integer.valueOf(index); } }); assertNotNull(list.get(5)); assertEquals(6, list.size()); assertNotNull(list.get(5)); assertEqual...
ListUtils { public static <T> List<T> emptyIfNull(final List<T> list) { return list == null ? Collections.<T>emptyList() : list; } private ListUtils(); static List<T> emptyIfNull(final List<T> list); static List<T> defaultIfNull(final List<T> list, final List<T> defaultList); static List<E> intersection(final List<? e...
@Test public void testEmptyIfNull() { assertTrue(ListUtils.emptyIfNull(null).isEmpty()); final List<Long> list = new ArrayList<Long>(); assertSame(list, ListUtils.emptyIfNull(list)); }
ListUtils { public static <T> List<T> defaultIfNull(final List<T> list, final List<T> defaultList) { return list == null ? defaultList : list; } private ListUtils(); static List<T> emptyIfNull(final List<T> list); static List<T> defaultIfNull(final List<T> list, final List<T> defaultList); static List<E> intersection(...
@Test public void testDefaultIfNull() { assertTrue(ListUtils.defaultIfNull(null, Collections.emptyList()).isEmpty()); final List<Long> list = new ArrayList<Long>(); assertSame(list, ListUtils.defaultIfNull(list, Collections.<Long>emptyList())); }
ListUtils { public static boolean isEqualList(final Collection<?> list1, final Collection<?> list2) { if (list1 == list2) { return true; } if (list1 == null || list2 == null || list1.size() != list2.size()) { return false; } final Iterator<?> it1 = list1.iterator(); final Iterator<?> it2 = list2.iterator(); Object obj1...
@Test public void testEquals() { final Collection<String> data = Arrays.asList("a", "b", "c"); final List<String> a = new ArrayList<String>( data ); final List<String> b = new ArrayList<String>( data ); assertEquals(true, a.equals(b)); assertEquals(true, ListUtils.isEqualList(a, b)); a.clear(); assertEquals(false, List...
ListUtils { public static int hashCodeForList(final Collection<?> list) { if (list == null) { return 0; } int hashCode = 1; final Iterator<?> it = list.iterator(); while (it.hasNext()) { final Object obj = it.next(); hashCode = 31 * hashCode + (obj == null ? 0 : obj.hashCode()); } return hashCode; } private ListUtils(...
@Test public void testHashCode() { final Collection<String> data = Arrays.asList("a", "b", "c"); final List<String> a = new ArrayList<String>(data); final List<String> b = new ArrayList<String>(data); assertEquals(true, a.hashCode() == b.hashCode()); assertEquals(true, a.hashCode() == ListUtils.hashCodeForList(a)); ass...
ListUtils { public static <E> List<E> retainAll(final Collection<E> collection, final Collection<?> retain) { final List<E> list = new ArrayList<E>(Math.min(collection.size(), retain.size())); for (final E obj : collection) { if (retain.contains(obj)) { list.add(obj); } } return list; } private ListUtils(); static Lis...
@Test public void testRetainAll() { final List<String> sub = new ArrayList<String>(); sub.add(a); sub.add(b); sub.add(x); final List<String> retained = ListUtils.retainAll(fullList, sub); assertTrue(retained.size() == 2); sub.remove(x); assertTrue(retained.equals(sub)); fullList.retainAll(sub); assertTrue(retained.equa...
ListUtils { public static <E> List<E> removeAll(final Collection<E> collection, final Collection<?> remove) { final List<E> list = new ArrayList<E>(); for (final E obj : collection) { if (!remove.contains(obj)) { list.add(obj); } } return list; } private ListUtils(); static List<T> emptyIfNull(final List<T> list); sta...
@Test public void testRemoveAll() { final List<String> sub = new ArrayList<String>(); sub.add(a); sub.add(b); sub.add(x); final List<String> remainder = ListUtils.removeAll(fullList, sub); assertTrue(remainder.size() == 3); fullList.removeAll(sub); assertTrue(remainder.equals(fullList)); try { ListUtils.removeAll(null,...
ListUtils { public static <E> List<E> subtract(final List<E> list1, final List<? extends E> list2) { final ArrayList<E> result = new ArrayList<E>(); final HashBag<E> bag = new HashBag<E>(list2); for (final E e : list1) { if (!bag.remove(e, 1)) { result.add(e); } } return result; } private ListUtils(); static List<T> e...
@Test public void testSubtract() { final List<String> list = new ArrayList<String>(); list.add(a); list.add(b); list.add(a); list.add(x); final List<String> sub = new ArrayList<String>(); sub.add(a); final List<String> result = ListUtils.subtract(list, sub); assertTrue(result.size() == 3); final List<String> expected =...
ListUtils { public static <E> int indexOf(final List<E> list, final Predicate<E> predicate) { if (list != null && predicate != null) { for (int i = 0; i < list.size(); i++) { final E item = list.get(i); if (predicate.evaluate(item)) { return i; } } } return -1; } private ListUtils(); static List<T> emptyIfNull(final L...
@Test public void testIndexOf() { Predicate<String> testPredicate = EqualPredicate.equalPredicate("d"); int index = ListUtils.indexOf(fullList, testPredicate); assertEquals(d, fullList.get(index)); testPredicate = EqualPredicate.equalPredicate("de"); index = ListUtils.indexOf(fullList, testPredicate); assertEquals(inde...
ListUtils { public static <E> List<E> longestCommonSubsequence(final List<E> a, final List<E> b) { return longestCommonSubsequence( a, b, DefaultEquator.defaultEquator() ); } private ListUtils(); static List<T> emptyIfNull(final List<T> list); static List<T> defaultIfNull(final List<T> list, final List<T> defaultList)...
@Test @SuppressWarnings("boxing") public void testLongestCommonSubsequence() { try { ListUtils.longestCommonSubsequence((List<?>) null, null); fail("failed to check for null argument"); } catch (final NullPointerException e) {} try { ListUtils.longestCommonSubsequence(Arrays.asList('A'), null); fail("failed to check fo...
ListUtils { public static <T> List<List<T>> partition(final List<T> list, final int size) { if (list == null) { throw new NullPointerException("List must not be null"); } if (size <= 0) { throw new IllegalArgumentException("Size must be greater than 0"); } return new Partition<T>(list, size); } private ListUtils(); st...
@Test @SuppressWarnings("boxing") public void testPartition() { final List<Integer> strings = new ArrayList<Integer>(); for (int i = 0; i <= 6; i++) { strings.add(i); } final List<List<Integer>> partition = ListUtils.partition(strings, 3); assertNotNull(partition); assertEquals(3, partition.size()); assertEquals(1, par...
ListUtils { public static <E> List<E> select(final Collection<? extends E> inputCollection, final Predicate<? super E> predicate) { return CollectionUtils.select(inputCollection, predicate, new ArrayList<E>(inputCollection.size())); } private ListUtils(); static List<T> emptyIfNull(final List<T> list); static List<T> ...
@Test @SuppressWarnings("boxing") public void testSelect() { final List<Integer> list = new ArrayList<Integer>(); list.add(1); list.add(2); list.add(3); list.add(4); final List<Integer> output1 = ListUtils.select(list, EQUALS_TWO); final List<Number> output2 = ListUtils.<Number>select(list, EQUALS_TWO); final HashSet<N...
UpdateOperator extends AbstractOperator { @Override public Object execute(Object[] values, InvocationStat stat) { InvocationContext context = invocationContextFactory.newInvocationContext(values); return execute(context, stat); } UpdateOperator(ASTRootNode rootNode, MethodDescriptor md, Config config); @Override Object...
@Test public void testUpdate() throws Exception { TypeToken<User> pt = TypeToken.of(User.class); TypeToken<Integer> rt = TypeToken.of(int.class); String srcSql = "update user set name=:1.name where id=:1.id"; AbstractOperator operator = getOperator(pt, rt, srcSql); operator.setJdbcOperations(new JdbcOperationsAdapter()...