Skip to content
This repository has been archived by the owner on Nov 9, 2019. It is now read-only.

Commit

Permalink
- addresses PR review
Browse files Browse the repository at this point in the history
- keys and tags have their own classes now
  • Loading branch information
mbabadi committed Jun 6, 2017
1 parent 65b124d commit d6a5bdf
Show file tree
Hide file tree
Showing 12 changed files with 1,121 additions and 941 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,32 @@
*
* @author Mehrtash Babadi <[email protected]>
*/
abstract class CacheNode {
public abstract class CacheNode {
/**
* A string identifier for the cache node
* A key for identifying this cache node
*/
private final String key;
private final NodeKey key;

/**
* The collection of string identifiers of the immediate parents of this node (can be empty)
* The collection of lookup keys for parents of this node (can be empty)
*/
private final Collection<String> parents;
private final Collection<NodeKey> parents;

/**
* The collection of string identifiers of the tags associated to this node (can be empty)
* The collection of tags associated to this node (can be empty)
*/
private final Collection<String> tags;
private final Collection<NodeTag> tags;

/**
* Public constructor
*
* @param key string identifier of the cache node
* @param key lookup key of the cache node
* @param tags the tags associated to this cache node
* @param parents immediate parents of this cache node
* @param parents lookup keys of the parents of this cache node
*/
CacheNode(@Nonnull final String key,
@Nonnull final Collection<String> tags,
@Nonnull final Collection<String> parents) {
CacheNode(@Nonnull final NodeKey key,
@Nonnull final Collection<NodeTag> tags,
@Nonnull final Collection<NodeKey> parents) {
this.key = Utils.nonNull(key, "The key of a cache node can not be null");
this.tags = Collections.unmodifiableCollection(Utils.nonNull(tags, "The tag collection of a cache node can not be null"));
this.parents = Collections.unmodifiableCollection(Utils.nonNull(parents, "The immediate parents of a cache node can not be null"));
Expand All @@ -47,10 +47,10 @@ abstract class CacheNode {
/**
* Get the value stored in the node
*
* @param parents parent values (as a map from their string identifiers to their values)
* @param parents parent values (as a map from their keys to their values)
* @return a {@link Duplicable}; possibly by reference
*/
abstract Duplicable get(@Nonnull final Map<String, Duplicable> parents);
abstract Duplicable get(@Nonnull final Map<NodeKey, Duplicable> parents);

/**
* Set the value of the node
Expand Down Expand Up @@ -95,27 +95,27 @@ abstract class CacheNode {
* Get the string identifier of the node
* @return a non-null {@link String}
*/
final String getKey() {
final NodeKey getKey() {
return key;
}

/**
* Get the collection of string identifier of the parents of this node (can be empty)
* Get the collection of keys of the parents of this node (can be empty)
*/
final Collection<String> getParents() {
final Collection<NodeKey> getParents() {
return Collections.unmodifiableCollection(parents);
}

/**
* Get the collection of string identifier of the tags associated to this node (can be empty)
* Get the collection of tags associated to this node (can be empty)
*/
final Collection<String> getTags() {
final Collection<NodeTag> getTags() {
return Collections.unmodifiableCollection(tags);
}

@Override
public final String toString() {
return key;
return key.toString();
}

/**
Expand All @@ -136,4 +136,66 @@ public final boolean equals(Object other) {
public final int hashCode() {
return key.hashCode();
}

/**
* This class represents a node key. It is a wrapper around a String.
*/
public static class NodeKey {
private final String key;

public NodeKey(final String key) {
this.key = Utils.nonNull(key, "Node key must be non-null");
}

@Override
public String toString() {
return key;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

NodeKey nodeKey = (NodeKey) o;

return key.equals(nodeKey.key);
}

@Override
public int hashCode() {
return key.hashCode();
}
}

/**
* This class represents a node tag. It is a wrapper around a String.
*/
public static class NodeTag {
private final String tag;

public NodeTag(final String tag) {
this.tag = Utils.nonNull(tag, "Node tag must be non-null");
}

@Override
public String toString() {
return tag;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

NodeTag nodeTag = (NodeTag) o;

return tag.equals(nodeTag.tag);
}

@Override
public int hashCode() {
return tag.hashCode();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ final class ComputableCacheNode extends CacheNode {
* Public constructor
*
* @param key the key of the node
* @param parents immediate parents of the node
* @param parents parents of the node
* @param func a function from a map that (at least) contains parents data to the computed value of this node
* @param isCaching does it store the value or not
*/
ComputableCacheNode(@Nonnull final String key,
@Nonnull final Collection<String> tags,
@Nonnull final Collection<String> parents,
ComputableCacheNode(@Nonnull final NodeKey key,
@Nonnull final Collection<NodeTag> tags,
@Nonnull final Collection<NodeKey> parents,
@Nullable final ComputableNodeFunction func,
final boolean isCaching) {
super(key, tags, parents);
Expand All @@ -41,9 +41,9 @@ final class ComputableCacheNode extends CacheNode {
isCacheCurrent = false;
}

private ComputableCacheNode(@Nonnull final String key,
@Nonnull final Collection<String> tags,
@Nonnull final Collection<String> parents,
private ComputableCacheNode(@Nonnull final NodeKey key,
@Nonnull final Collection<NodeTag> tags,
@Nonnull final Collection<NodeKey> parents,
@Nullable final ComputableNodeFunction func,
final boolean isCaching,
final Duplicable cachedValue,
Expand Down Expand Up @@ -93,7 +93,7 @@ void set(@Nullable final Duplicable val) {
* @throws ComputableNodeFunction.ParentValueNotFoundException if a required parent value is not given
*/
@Override
Duplicable get(@Nonnull final Map<String, Duplicable> parentsValues)
Duplicable get(@Nonnull final Map<NodeKey, Duplicable> parentsValues)
throws ComputableNodeFunction.ParentValueNotFoundException, ExternallyComputableNodeValueUnavailableException {
if (hasValue()) {
return cachedValue;
Expand All @@ -119,6 +119,7 @@ ComputableCacheNode duplicate() {
* @param newValue the cache value to be replaced with the old value
* @return a new instance of {@link ComputableCacheNode}
*/
@Override
ComputableCacheNode duplicateWithUpdatedValue(final Duplicable newValue) {
if (isCaching && newValue != null && newValue.hasValue()) {
return new ComputableCacheNode(getKey(), getTags(), getParents(), func, true, newValue, true);
Expand Down Expand Up @@ -153,7 +154,7 @@ static final class ExternallyComputableNodeValueUnavailableException extends Run
implements Serializable {
private static final long serialVersionUID = 9056196660803073912L;

private ExternallyComputableNodeValueUnavailableException(final String nodeKey) {
private ExternallyComputableNodeValueUnavailableException(final NodeKey nodeKey) {
super(String.format("Either the externally mutable node \"%s\" is not initialized or is outdated",
nodeKey));
}
Expand Down
Loading

0 comments on commit d6a5bdf

Please sign in to comment.