public boolean contains(int index, int size) { if (!isInBound(index, size)) { return false; } final int p = toPositiveIndex(index, size); return p >= actualLeft(size) && p <= actualRight(size); }
private static boolean isInBound(int i, int size) { final int p = toPositiveIndex(i, size); return p >= 0 && p < size; } }
public boolean shouldExpand(Expandable expandable) { for (ExpandParameter expandParameter : expandParameters) { if (expandParameter.shouldExpand(expandable)) { return true; } } return false; }
private static void appendParam(Map<String, ExpandInformation> parameters, String expand) { final ExpandKey key = ExpandKey.from(StringUtils.substringBefore(expand, DOT)); String newParameter = StringUtils.substringAfter(expand, DOT); DefaultExpandParameter existingParameter = null; if (parameters.containsKey(key.getName())) { existingParameter = parameters.get(key.getName()).getExpandParameter(); } else { existingParameter = new DefaultExpandParameter(); parameters.put(key.getName(), new ExpandInformation(key.getIndexes(), existingParameter)); } if (StringUtils.isNotBlank(newParameter)) { appendParam(existingParameter.parameters, newParameter); } }
public ExpandParameter getExpandParameter(Expandable expandable) { final Collection<ExpandParameter> newExpandParameters = new LinkedList<ExpandParameter>(); for (ExpandParameter expandParameter : expandParameters) { newExpandParameters.add(expandParameter.getExpandParameter(expandable)); } return new ChainingExpandParameter(newExpandParameters); }
public Indexes getIndexes(Expandable expandable) { // we do not merge indexes, // so if we find an IndexParser.ALL that's what we return // if we find only one non-empty, that's what we return // else we throw an exception Indexes indexes = null; for (ExpandParameter expandParameter : expandParameters) { final Indexes i = expandParameter.getIndexes(expandable); if (i.equals(IndexParser.ALL)) { return IndexParser.ALL; } if (!i.equals(IndexParser.EMPTY)) { if (indexes == null) { indexes = i; } else { throw new IndexException("Cannot merge multiple indexed expand parameters."); } } } return indexes != null ? indexes : IndexParser.EMPTY; }
private static Map<String, ExpandInformation> parse(Collection<String> expands) { final Map<String, ExpandInformation> parameters = Maps.newHashMap(); for (String expand : preProcess(expands)) { if (StringUtils.isNotEmpty(expand)) { appendParam(parameters, expand); } } return parameters; }
private static ExpandKey from(String key) { final Matcher keyMatcher = KEY_PATTERN.matcher(key); if (!keyMatcher.matches()) { throw new RuntimeException("key <" + key + "> doesn't match pattern"); } final String name = keyMatcher.group(1); final String indexesString = keyMatcher.group(2); return new ExpandKey(name, IndexParser.parse(indexesString)); } }
public ExpandParameter getExpandParameter(Expandable expandable) { final ExpandInformation wildcardExpandInformation = parameters.get(WILDCARD); final ExpandInformation valueExpandInformation = parameters.get(Preconditions.checkNotNull(expandable).value()); return new ChainingExpandParameter( wildcardExpandInformation != null ? wildcardExpandInformation.getExpandParameter() : EMPTY_EXPAND_PARAMETER, valueExpandInformation != null ? valueExpandInformation.getExpandParameter() : EMPTY_EXPAND_PARAMETER); }
public List<T> getItems(Indexes indexes) { final List<T> toReturn = Lists.newLinkedList(); for (Integer i : indexes.getIndexes(items.size())) { if (i < items.size()) { toReturn.add(items.get(i)); } if (toReturn.size() == maxResults) { break; } } return toReturn; } };
static Indexes parse(String indexes) { if (StringUtils.isBlank(indexes)) { return ALL; } else if (INDEX_PATTERN.matcher(indexes).matches()) { return new SimpleIndexes(Integer.parseInt(indexes)); } else if (RANGE_PATTERN.matcher(indexes).matches()) { final String leftAsString = StringUtils.substringBefore(indexes, ":"); final String rightAsString = StringUtils.substringAfter(indexes, ":"); return new RangeIndexes( StringUtils.isNotBlank(leftAsString) ? Integer.parseInt(leftAsString) : null, StringUtils.isNotBlank(rightAsString) ? Integer.parseInt(rightAsString) : null); } else { return EMPTY; } }
public List<T> getItems(Indexes indexes) { final int startIndex = indexes.getMinIndex(size); if (startIndex != -1) { setStartIndex(startIndex); } return getPagingCallback().getItems(indexes); } };
public int getMinIndex(int size) { return getIndex(size); }
public boolean isEmpty() { for (ExpandParameter expandParameter : expandParameters) { if (!expandParameter.isEmpty()) { return false; } } return true; } }
public int getMaxIndex(int size) { return actualRight(size); }
public int getMinIndex(int size) { return actualLeft(size); }
public DefaultExpandParameter(Collection<String> expands) { parameters = parse(expands != null ? expands : Collections.<String>emptyList()); }
public Indexes getIndexes(Expandable expandable) { final ExpandInformation expandInformation = parameters.get(Preconditions.checkNotNull(expandable).value()); return expandInformation != null ? expandInformation.getIndexes() : IndexParser.EMPTY; }
public SortedSet<Integer> getIndexes(int size) { final SortedSet<Integer> allIndexes = Sets.newTreeSet(); final int actualLeft = actualLeft(size); final int actualRight = actualRight(size); if (actualLeft != -1 && actualRight != -1) { for (int i = actualLeft; i <= actualRight; i++) { allIndexes.add(i); } } return allIndexes; }
public int getMaxIndex(int size) { return getIndex(size); }