-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
...-core/src/main/java/com/github/xdcrafts/flower/core/impl/middleware/ToggleMiddleware.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright (c) 2017 Vadim Dubs https://github.com/xdcrafts | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, | ||
* either express or implied. See the License for the specific | ||
* language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package com.github.xdcrafts.flower.core.impl.middleware; | ||
|
||
import com.github.xdcrafts.flower.core.Middleware; | ||
|
||
import java.util.Map; | ||
import java.util.function.Function; | ||
import java.util.function.Supplier; | ||
|
||
/** | ||
* Toggle middleware. | ||
*/ | ||
public class ToggleMiddleware implements Middleware, Supplier<Boolean> { | ||
|
||
private final String name; | ||
private volatile boolean isEnabled; | ||
|
||
public ToggleMiddleware(String name) { | ||
this.name = name; | ||
} | ||
|
||
public ToggleMiddleware(String name, boolean isEnabled) { | ||
this.name = name; | ||
this.isEnabled = isEnabled; | ||
} | ||
|
||
public void setEnabled(boolean enabled) { | ||
this.isEnabled = enabled; | ||
} | ||
|
||
public void setEnabled(String enabled) { | ||
this.isEnabled = Boolean.valueOf(enabled); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return this.name; | ||
} | ||
|
||
@Override | ||
public Boolean get() { | ||
return this.isEnabled; | ||
} | ||
|
||
@Override | ||
public Function<Map, Map> apply(Map<String, Object> stringObjectMap, Function<Map, Map> mapMapFunction) { | ||
return ctx -> this.isEnabled ? mapMapFunction.apply(ctx) : ctx; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "ToggleMiddleware{" | ||
+ "name='" + name + '\'' | ||
+ ", isEnabled=" + isEnabled | ||
+ '}'; | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
flower-core/src/main/java/com/github/xdcrafts/flower/core/impl/selectors/Toggle.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright (c) 2017 Vadim Dubs https://github.com/xdcrafts | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, | ||
* either express or implied. See the License for the specific | ||
* language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package com.github.xdcrafts.flower.core.impl.selectors; | ||
|
||
import java.util.Map; | ||
import java.util.function.Predicate; | ||
|
||
/** | ||
* Toggle predicate. | ||
*/ | ||
public class Toggle implements Predicate<Map> { | ||
|
||
private volatile boolean isEnabled = false; | ||
|
||
public Toggle() { | ||
} | ||
|
||
public Toggle(boolean isEnabled) { | ||
this.isEnabled = isEnabled; | ||
} | ||
|
||
public void setEnabled(boolean enabled) { | ||
isEnabled = enabled; | ||
} | ||
|
||
public void setEnabled(String enabled) { | ||
this.isEnabled = Boolean.valueOf(enabled); | ||
} | ||
|
||
@Override | ||
public boolean test(Map map) { | ||
return this.isEnabled; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Toggle{" | ||
+ "isEnabled=" + this.isEnabled | ||
+ '}'; | ||
} | ||
} | ||
|