-
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
3 changed files
with
148 additions
and
5 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
flower-core/src/main/java/com/github/xdcrafts/flower/core/Actor.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,44 @@ | ||
/* | ||
* 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; | ||
|
||
import java.util.Map; | ||
import java.util.function.Consumer; | ||
import java.util.function.Function; | ||
|
||
/** | ||
* Actor is a function that takes function, initializing action context, and provides result of type T. | ||
* @param <T> return type | ||
*/ | ||
public interface Actor<T> extends Function<Consumer<Map>, T> { | ||
|
||
/** | ||
* Apply actor's action to map context initialized by supported closure. | ||
* @param contextInitializer closure that fills context with data to process | ||
* @return value of type T produced from map context | ||
*/ | ||
@Override | ||
T apply(Consumer<Map> contextInitializer); | ||
|
||
/** | ||
* Call apply without any initializer function provided. | ||
*/ | ||
default T apply() { | ||
return apply(ctx -> { | ||
}); | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
flower-core/src/main/java/com/github/xdcrafts/flower/core/impl/DefaultActor.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,75 @@ | ||
/* | ||
* 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; | ||
|
||
import com.github.xdcrafts.flower.core.Action; | ||
import com.github.xdcrafts.flower.core.Actor; | ||
|
||
import java.util.Map; | ||
import java.util.function.Consumer; | ||
import java.util.function.Function; | ||
import java.util.function.Supplier; | ||
|
||
/** | ||
* Default implementation of Actor. | ||
* @param <T> return type; | ||
*/ | ||
public class DefaultActor<T> implements Actor<T> { | ||
|
||
private final Supplier<Map> contextFactory; | ||
private final Action action; | ||
private final Function<Map, T> conclusion; | ||
private final Function<Map, T> body; | ||
|
||
public DefaultActor( | ||
Supplier<Map> contextFactory, | ||
Action action, | ||
Function<Map, T> conclusion | ||
) { | ||
if (contextFactory == null) { | ||
throw new IllegalArgumentException("ContextFactory can not be null."); | ||
} | ||
if (action == null) { | ||
throw new IllegalArgumentException("Action can not be null."); | ||
} | ||
if (conclusion == null) { | ||
throw new IllegalArgumentException("Conclusion can not be null."); | ||
} | ||
this.contextFactory = contextFactory; | ||
this.action = action; | ||
this.conclusion = conclusion; | ||
this.body = this.action.andThen(this.conclusion); | ||
} | ||
|
||
@Override | ||
public T apply(Consumer<Map> contextInitializer) { | ||
final Map ctx = this.contextFactory.get(); | ||
if (contextInitializer != null) { | ||
contextInitializer.accept(ctx); | ||
} | ||
return this.body.apply(ctx); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "DefaultActor{" | ||
+ "contextFactory=" + this.contextFactory | ||
+ ", action=" + this.action | ||
+ ", conclusion=" + this.conclusion | ||
+ '}'; | ||
} | ||
} |
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