-
Notifications
You must be signed in to change notification settings - Fork 179
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
base: master
Are you sure you want to change the base?
Changes from all commits
8182c82
0fac88c
b0e47d0
c51ed37
efac56b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
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 | ||
|
@@ -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"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} |
There was a problem hiding this comment.
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>?