Skip to content

Commit

Permalink
replace guava with java8 equivalents
Browse files Browse the repository at this point in the history
  • Loading branch information
jdillon committed Jun 10, 2017
1 parent 3f139c8 commit a70fe35
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 70 deletions.
6 changes: 0 additions & 6 deletions style/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,6 @@
<version>2.0.0</version>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>20.0</version>
</dependency>

<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
Expand Down
22 changes: 10 additions & 12 deletions style/src/main/java/org/jline/style/MemoryStyleSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@

import javax.annotation.Nullable;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.planet57.gossip.Log;
import org.slf4j.Logger;

import static com.google.common.base.Preconditions.checkNotNull;
import static java.util.Objects.requireNonNull;

/**
* In-memory {@link StyleSource}.
Expand Down Expand Up @@ -47,25 +45,25 @@ public String get(final String group, final String name) {

@Override
public void set(final String group, final String name, final String style) {
checkNotNull(group);
checkNotNull(name);
checkNotNull(style);
requireNonNull(group);
requireNonNull(name);
requireNonNull(style);
backing.computeIfAbsent(group, k -> new ConcurrentHashMap<>()).put(name, style);
log.trace("Set: [{}] {} -> {}", group, name, style);
}

@Override
public void remove(final String group) {
checkNotNull(group);
requireNonNull(group);
if (backing.remove(group) != null) {
log.trace("Removed: [{}]");
}
}

@Override
public void remove(final String group, final String name) {
checkNotNull(group);
checkNotNull(name);
requireNonNull(group);
requireNonNull(name);
Map<String,String> styles = backing.get(group);
if (styles != null) {
styles.remove(name);
Expand All @@ -81,16 +79,16 @@ public void clear() {

@Override
public Iterable<String> groups() {
return ImmutableSet.copyOf(backing.keySet());
return Collections.unmodifiableSet(backing.keySet());
}

@Override
public Map<String,String> styles(final String group) {
checkNotNull(group);
requireNonNull(group);
Map<String,String> result = backing.get(group);
if (result == null) {
result = Collections.emptyMap();
}
return ImmutableMap.copyOf(result);
return Collections.unmodifiableMap(result);
}
}
25 changes: 11 additions & 14 deletions style/src/main/java/org/jline/style/NopStyleSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@

import javax.annotation.Nullable;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;

import static com.google.common.base.Preconditions.checkNotNull;
import static java.util.Objects.requireNonNull;

/**
* {@link StyleSource} which always returns {@code null}.
Expand All @@ -34,8 +31,8 @@ public class NopStyleSource
@Nullable
@Override
public String get(final String group, final String name) {
checkNotNull(group);
checkNotNull(name);
requireNonNull(group);
requireNonNull(name);
return null;
}

Expand All @@ -44,26 +41,26 @@ public String get(final String group, final String name) {
*/
@Override
public void set(final String group, final String name, final String style) {
checkNotNull(group);
checkNotNull(name);
checkNotNull(style);
requireNonNull(group);
requireNonNull(name);
requireNonNull(style);
}

/**
* Non-operation.
*/
@Override
public void remove(final String group) {
checkNotNull(group);
requireNonNull(group);
}

/**
* Non-operation.
*/
@Override
public void remove(final String group, final String name) {
checkNotNull(group);
checkNotNull(name);
requireNonNull(group);
requireNonNull(name);
}

/**
Expand All @@ -79,14 +76,14 @@ public void clear() {
*/
@Override
public Iterable<String> groups() {
return ImmutableList.copyOf(Collections.emptyList());
return Collections.unmodifiableList(Collections.emptyList());
}

/**
* Always returns empty map.
*/
@Override
public Map<String, String> styles(final String group) {
return ImmutableMap.copyOf(Collections.emptyMap());
return Collections.unmodifiableMap(Collections.emptyMap());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

import javax.annotation.Nullable;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Strings;
import com.planet57.gossip.Log;
import org.jline.style.StyleBundle.DefaultStyle;
import org.jline.style.StyleBundle.StyleGroup;
Expand All @@ -24,7 +22,7 @@
import org.jline.utils.AttributedStyle;
import org.slf4j.Logger;

import static com.google.common.base.Preconditions.checkNotNull;
import static java.util.Objects.requireNonNull;

/**
* {@link StyleBundle} proxy invocation-handler to convert method calls into string styling.
Expand All @@ -42,8 +40,8 @@ class StyleBundleInvocationHandler
private final StyleResolver resolver;

public StyleBundleInvocationHandler(final Class<? extends StyleBundle> type, final StyleResolver resolver) {
this.type = checkNotNull(type);
this.resolver = checkNotNull(resolver);
this.type = requireNonNull(type);
this.resolver = requireNonNull(resolver);
}

@Override
Expand Down Expand Up @@ -106,21 +104,28 @@ public String toString() {
// Helpers
//

private static String emptyToNull(final String value) {
if (value == null || value.isEmpty()) {
return null;
}
return value;
}

/**
* Returns the style group-name for given type, or {@code null} if unable to determine.
*/
@Nullable
private static String getStyleGroup(final Class<?> type) {
StyleGroup styleGroup = type.getAnnotation(StyleGroup.class);
return styleGroup != null ? Strings.emptyToNull(styleGroup.value().trim()) : null;
return styleGroup != null ? emptyToNull(styleGroup.value().trim()) : null;
}

/**
* Returns the style-name for given method, or {@code null} if unable to determine.
*/
private static String getStyleName(final Method method) {
StyleName styleName = method.getAnnotation(StyleName.class);
return styleName != null ? Strings.emptyToNull(styleName.value().trim()) : method.getName();
return styleName != null ? emptyToNull(styleName.value().trim()) : method.getName();
}

/**
Expand All @@ -130,7 +135,7 @@ private static String getStyleName(final Method method) {
private static String getDefaultStyle(final Method method) {
DefaultStyle defaultStyle = method.getAnnotation(DefaultStyle.class);
// allow whitespace in default-style.value, but disallow empty-string
return defaultStyle != null ? Strings.emptyToNull(defaultStyle.value()) : null;
return defaultStyle != null ? emptyToNull(defaultStyle.value()) : null;
}

//
Expand All @@ -144,8 +149,8 @@ private static String getDefaultStyle(final Method method) {
*/
@SuppressWarnings("unchecked")
static <T extends StyleBundle> T create(final StyleResolver resolver, final Class<T> type) {
checkNotNull(resolver);
checkNotNull(type);
requireNonNull(resolver);
requireNonNull(type);

log.trace("Using style-group: {} for type: {}", resolver.getGroup(), type.getName());

Expand All @@ -159,7 +164,7 @@ static <T extends StyleBundle> T create(final StyleResolver resolver, final Clas
* @see Styler#bundle(String,Class)
*/
static <T extends StyleBundle> T create(final StyleSource source, final Class<T> type) {
checkNotNull(type);
requireNonNull(type);

String group = getStyleGroup(type);
if (group == null) {
Expand All @@ -176,7 +181,7 @@ static <T extends StyleBundle> T create(final StyleSource source, final Class<T>
/**
* Thrown when {@link StyleBundle} method has missing {@link DefaultStyle}.
*/
@VisibleForTesting
// @VisibleForTesting
static class StyleBundleMethodMissingDefaultStyleException
extends RuntimeException
{
Expand All @@ -192,7 +197,7 @@ public StyleBundleMethodMissingDefaultStyleException(final Method method) {
/**
* Thrown when processing {@link StyleBundle} method is found to be invalid.
*/
@VisibleForTesting
// @VisibleForTesting
static class InvalidStyleBundleMethodException
extends RuntimeException
{
Expand All @@ -204,7 +209,7 @@ public InvalidStyleBundleMethodException(final Method method, final String messa
/**
* Thrown when looking up {@link StyleGroup} on a type found to be missing or invalid.
*/
@VisibleForTesting
// @VisibleForTesting
static class InvalidStyleGroupException
extends RuntimeException
{
Expand Down
8 changes: 4 additions & 4 deletions style/src/main/java/org/jline/style/StyleExpression.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import org.jline.utils.AttributedStringBuilder;
import org.jline.utils.AttributedStyle;

import static com.google.common.base.Preconditions.checkNotNull;
import static java.util.Objects.requireNonNull;

/**
* Provides {@code @{style value}} expression evaluation.
Expand All @@ -32,15 +32,15 @@ public class StyleExpression
private final StyleResolver resolver;

public StyleExpression(final StyleResolver resolver) {
this.resolver = checkNotNull(resolver);
this.resolver = requireNonNull(resolver);
}

/**
* Evaluate expression and append to buffer.
*/
public void evaluate(final AttributedStringBuilder buff, final String expression) {
checkNotNull(buff);
checkNotNull(expression);
requireNonNull(buff);
requireNonNull(expression);

String input = expression;
Matcher matcher = PATTERN.matcher(input);
Expand Down
16 changes: 8 additions & 8 deletions style/src/main/java/org/jline/style/StyleFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import org.jline.utils.AttributedString;
import org.jline.utils.AttributedStyle;

import static com.google.common.base.Preconditions.checkNotNull;
import static java.util.Objects.requireNonNull;

/**
* Factory to create styled strings.
Expand All @@ -23,14 +23,14 @@ public class StyleFactory
private final StyleResolver resolver;

public StyleFactory(final StyleResolver resolver) {
this.resolver = checkNotNull(resolver);
this.resolver = requireNonNull(resolver);
}

/**
* Encode string with style applying value.
*/
public AttributedString style(final String style, final String value) {
checkNotNull(value);
requireNonNull(value);
AttributedStyle astyle = resolver.resolve(style);
return new AttributedString(value, astyle);
}
Expand All @@ -41,8 +41,8 @@ public AttributedString style(final String style, final String value) {
* @see #style(String, String)
*/
public AttributedString style(final String style, final String format, final Object... params) {
checkNotNull(format);
checkNotNull(params);
requireNonNull(format);
requireNonNull(params);
// params may be empty
return style(style, String.format(format, params));
}
Expand All @@ -51,7 +51,7 @@ public AttributedString style(final String style, final String format, final Obj
* Evaluate a style expression.
*/
public AttributedString evaluate(final String expression) {
checkNotNull(expression);
requireNonNull(expression);
return new StyleExpression(resolver).evaluate(expression);
}

Expand All @@ -61,8 +61,8 @@ public AttributedString evaluate(final String expression) {
* @see #evaluate(String)
*/
public AttributedString evaluate(final String format, final Object... params) {
checkNotNull(format);
checkNotNull(params);
requireNonNull(format);
requireNonNull(params);
// params may be empty
return evaluate(String.format(format, params));
}
Expand Down
Loading

0 comments on commit a70fe35

Please sign in to comment.