@NotNull public static String addRequestParam (@NotNull String url, @NotNull Pair<String, ?>... properties) { StringBuilder sb = new StringBuilder(url); boolean containsOtherParams = true; if ( !url.contains("?") ) { sb.append("?"); containsOtherParams = false; } boolean isFirst = true; LoopData ld = new LoopData(properties); for (Pair<String, ?> property : properties) { ld.next(); if (!url.contains(property.getFirst())) { if (isFirst && containsOtherParams) { sb.append("&"); isFirst = false; } sb.append(property.getFirst()).append("=").append(property.getSecond()); if (!ld.isLast()) { sb.append("&"); } } } return sb.toString(); } }
@Nonnull public static String toString(@Nonnull Collection<Object> objects, @Nonnull String separator) { StringBuilder sb = new StringBuilder(); LoopData ld = new LoopData(objects); for (Object o : objects) { if (ld.isFirstAndNext()) { sb.append(separator); } sb.append(o); } return sb.toString(); }
/** * Indicates if it is the first time of usage of method or not (used in loops) * * @return 'true' if it is the first loop step, 'false' otherwise */ public boolean isFirstAndNext() { boolean result = index == 0; next(); return result; }