com.gemstone.gemfire.management.internal.web.domain.LinkIndex Maven / Gradle / Ivy
/*
* Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you
* may not use this file except in compliance with the License. You
* may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License. See accompanying
* LICENSE file.
*/
package com.gemstone.gemfire.management.internal.web.domain;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import com.gemstone.gemfire.internal.lang.StringUtils;
/**
* The LinkIndex class is abstraction for modeling an index of Links.
*
* @author John Blum
* @see javax.xml.bind.annotation.XmlRootElement
* @see com.gemstone.gemfire.management.internal.web.domain.Link
* @since 7.5
*/
@SuppressWarnings("unused")
@XmlRootElement(name = "link-index")
public class LinkIndex implements Iterable {
@XmlElement(name = "link")
private final Set links = new TreeSet();
public LinkIndex add(final Link link) {
assert link != null : "The Link to add to the index cannot be null!";
links.add(link);
return this;
}
public LinkIndex addAll(final Link... links) {
assert links != null : "The array of Links to add to this index cannot be null!";
return addAll(Arrays.asList(links));
}
public LinkIndex addAll(final Iterable links) {
assert links != null : "The Iterable collection of Links to add to this index cannot be null!";
for (final Link link : links) {
add(link);
}
return this;
}
public Link find(final String relation) {
Link linkFound = null;
for (Link link : this) {
if (link.getRelation().equalsIgnoreCase(relation)) {
linkFound = link;
break;
}
}
return linkFound;
}
public Link[] findAll(final String relation) {
final List links = new ArrayList();
for (final Link link : this) {
if (link.getRelation().equalsIgnoreCase(relation)) {
links.add(link);
}
}
return links.toArray(new Link[links.size()]);
}
public boolean isEmpty() {
return links.isEmpty();
}
@Override
public Iterator iterator() {
return Collections.unmodifiableSet(links).iterator();
}
public int size() {
return links.size();
}
public List toList() {
return new ArrayList(links);
}
public Map> toMap() {
final Map> links = new TreeMap>();
for (final Link link : this) {
List linksByRelation = links.get(link.getRelation());
if (linksByRelation == null) {
linksByRelation = new ArrayList(size());
links.put(link.getRelation(), linksByRelation);
}
linksByRelation.add(link);
}
return links;
}
@Override
public String toString() {
final StringBuilder buffer = new StringBuilder("[");
int count = 0;
for (final Link link : this) {
buffer.append(count++ > 0 ? ", " : StringUtils.EMPTY_STRING).append(link);
}
buffer.append("]");
return buffer.toString();
}
}