Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: ConditionalBinder #256

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,35 @@

import javax.inject.Singleton;

import com.google.inject.Binder;
import com.google.inject.Module;
import com.google.inject.Provides;
import com.netflix.governator.spi.PropertySource;

public class PropertiesPropertySource extends AbstractPropertySource {
public final class PropertiesPropertySource extends AbstractPropertySource implements Module {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gak. Properties is legacy Java with all synchronized methods. Why do we want this in 2016, and not a Map<String,String>?

private Properties props;

public PropertiesPropertySource(Properties props) {
this.props = props;
}

public PropertiesPropertySource() {
this(new Properties());
}

public static PropertiesPropertySource from(Properties props) {
return new PropertiesPropertySource(props);
}

public PropertiesPropertySource setProperty(String key, String value) {
props.setProperty(key, value);
return this;
}

public boolean hasProperty(String key, String value) {
return props.containsKey(key);
}

public static Module toModule(final Properties props) {
return new SingletonModule() {
@Provides
Expand All @@ -38,4 +52,28 @@ public String get(String key) {
public String get(String key, String defaultValue) {
return props.getProperty(key, defaultValue);
}

@Override
public void configure(Binder binder) {
binder.bind(PropertySource.class).toInstance(this);
}

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

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

throw new RuntimeException("Only one PropertiesModule may be installed");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IllegalStateException would be clearer; usually a bad idea to throw RuntimeException

}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.netflix.governator.conditional;

import java.util.ArrayList;
import java.util.List;

import com.google.inject.Injector;

/**
* Conditional that is true if and only if all child conditionals are true
*/
public class AllOfConditional implements Conditional {
private final List<Conditional> children;

public AllOfConditional(List<Conditional> children) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be able to take any Iterable or Collection as input, and add a varargs constructor too?

this.children = new ArrayList<>(children);
}

@Override
public boolean matches(Injector injector) {
for (Conditional conditional : children) {
if (!conditional.matches(injector)) {
return false;
}
}
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.netflix.governator.conditional;

import java.util.ArrayList;
import java.util.List;

import com.google.inject.Injector;

/**
* Conditional that is true if at least one of the child conditionals
* is true
*/
public class AnyOfConditional implements Conditional {
private final List<Conditional> children;

public AnyOfConditional(List<Conditional> children) {
this.children = new ArrayList<>(children);
}

@Override
public boolean matches(Injector injector) {
for (Conditional conditional : children) {
if (conditional.matches(injector)) {
return true;
}
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.netflix.governator.conditional;

import com.google.inject.Injector;

/**
* Contract for any conditional that may be applied to a conditional binding
* bound via {@link ConditionalBinder}.
*
* Dependencies needed by a concrete conditional should be injected using
* member injection.
*
*/
public interface Conditional {
/**
* Evaluate whether the condition is true. evaluate() is only called once at injector
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

javadoc update

* creation time.
* @return True if conditional is true otherwise false
*/
boolean matches(Injector injector);
}
Loading