added caching behaviour to link predicates

This commit is contained in:
danikov 2012-04-23 11:29:59 +01:00
parent 588a7c38ad
commit a1e2d6d047
1 changed files with 45 additions and 16 deletions

View File

@ -20,9 +20,13 @@ package org.jclouds.vcloud.director.v1_5.predicates;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import org.jclouds.vcloud.director.v1_5.VCloudDirectorMediaType;
import org.jclouds.vcloud.director.v1_5.domain.Link; import org.jclouds.vcloud.director.v1_5.domain.Link;
import com.google.common.base.Predicate; import com.google.common.base.Predicate;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
/** /**
* Predicates handy when working with Links * Predicates handy when working with Links
@ -31,7 +35,7 @@ import com.google.common.base.Predicate;
*/ */
public class LinkPredicates { public class LinkPredicates {
/** /**
* matches links of the given relation * matches links of the given relation
* *
@ -46,32 +50,57 @@ public class LinkPredicates {
/** @see #relEquals(String) */ /** @see #relEquals(String) */
public static Predicate<Link> relEquals(final Link.Rel rel) { public static Predicate<Link> relEquals(final Link.Rel rel) {
checkNotNull(rel, "rel must be defined"); return LINK_REL_SELECTORS.apply(checkNotNull(rel, "rel must be defined"));
return new Predicate<Link>() {
@Override
public boolean apply(Link link) {
return rel == link.getRel();
}
@Override
public String toString() {
return "relEquals(" + rel.value() + ")";
}
};
} }
private static final LoadingCache<Link.Rel, Predicate<Link>> LINK_REL_SELECTORS = CacheBuilder.newBuilder()
.maximumSize(Link.Rel.ALL.size())
.build(
new CacheLoader<Link.Rel, Predicate<Link>>() {
public Predicate<Link> load(final Link.Rel rel) {
return new Predicate<Link>() {
@Override
public boolean apply(Link link) {
return rel == link.getRel();
}
@Override
public String toString() {
return "relEquals(" + rel.value() + ")";
}
};
}
});
/** /**
* @see ReferenceTypePredicates#nameEquals * @see ReferenceTypePredicates#nameEquals
*/ */
public static Predicate<Link> nameEquals(String name) { public static Predicate<Link> nameEquals(String name) {
return ReferencePredicates.nameEquals(name); return MEDIA_NAME_SELECTORS.apply(name);
} }
private static final LoadingCache<String, Predicate<Link>> MEDIA_NAME_SELECTORS = CacheBuilder.newBuilder()
.maximumSize(VCloudDirectorMediaType.ALL.size())
.build(
new CacheLoader<String, Predicate<Link>>() {
public Predicate<Link> load(String key) {
return ReferencePredicates.nameEquals(key);
}
});
/** /**
* @see ReferenceTypePredicates#typeEquals * @see ReferenceTypePredicates#typeEquals
*/ */
public static Predicate<Link> typeEquals(String type) { public static Predicate<Link> typeEquals(String type) {
return ReferencePredicates.typeEquals(type); return MEDIA_TYPE_SELECTORS.apply(type);
} }
private static final LoadingCache<String, Predicate<Link>> MEDIA_TYPE_SELECTORS = CacheBuilder.newBuilder()
.maximumSize(VCloudDirectorMediaType.ALL.size())
.build(
new CacheLoader<String, Predicate<Link>>() {
public Predicate<Link> load(String key) {
return ReferencePredicates.typeEquals(key);
}
});
} }