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

Commit

Permalink
Introduce Substitutable
Browse files Browse the repository at this point in the history
  • Loading branch information
pdolezal committed Mar 22, 2016
1 parent 986fa9e commit f7b09d2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
<dependency>
<groupId>net.yetamine</groupId>
<artifactId>net.yetamine.sova</artifactId>
<!-- Built with snapshot/1.0.0/20160317 -->
<!-- Built with snapshot/1.0.0/20160322 -->
<version>1.0.0-SNAPSHOT</version>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import java.util.Objects;

import net.yetamine.sova.AdaptationProvider;
import net.yetamine.sova.Mappable;
import net.yetamine.sova.Substitutable;
import net.yetamine.sova.symbols.DelegatingSymbol;

/**
Expand All @@ -28,10 +30,12 @@
* @param <T>
* the type of resulting values
*/
public abstract class ServletAttributeSymbol<T> extends DelegatingSymbol<T> {
public abstract class ServletAttributeSymbol<T> extends DelegatingSymbol<T> implements Substitutable<Mappable<String, T>> {

/** Name of the attribute. */
private final String attribute;
/** Cached {@link #substitute()}. */
private Mappable<String, T> substitute;

/**
* Prepares a new instance.
Expand Down Expand Up @@ -86,4 +90,21 @@ public static String name(Class<?> qualifier, String identifier) {
public final String attribute() {
return attribute;
}

/**
* @see net.yetamine.sova.symbols.PublicSymbol#substitute()
*/
public final Mappable<String, T> substitute() {
// Using caching technique that uses out-of-thin air thread safety;
// this technique is alright here, because the instances are always
// behaving in the same way, therefore they are interchangeable
Mappable<String, T> result = substitute;
if (result != null) {
return result;
}

result = Mappable.of(attribute, this);
substitute = result;
return result;
}
}
6 changes: 1 addition & 5 deletions src/main/java/net/yetamine/sova/servlet/ServletSymbol.java
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,7 @@ public T use(ServletRequest source) {
*/
public Optional<T> find(ServletRequest source) {
final T result = requestSymbol.get(source);
if (result != null) {
return Optional.of(result);
}

return contextSymbol.find(source.getServletContext());
return (result != null) ? Optional.of(result) : contextSymbol.find(source.getServletContext());
}

/**
Expand Down

0 comments on commit f7b09d2

Please sign in to comment.