-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GH-527 Remove InterceptorData and LocalCache and delegate its functio…
…nalities to LocalChannel data
- Loading branch information
Showing
64 changed files
with
511 additions
and
519 deletions.
There are no files selected for viewing
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
97 changes: 97 additions & 0 deletions
97
...mework/src/main/java/org/panda_lang/framework/design/interpreter/parser/LocalChannel.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,97 @@ | ||
/* | ||
* Copyright (c) 2020 Dzikoysk | ||
* | ||
* 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 org.panda_lang.framework.design.interpreter.parser; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* The communication channel between {@link org.panda_lang.framework.design.interpreter.parser.pipeline.Handler} and parser. | ||
* In some cases, it may be helpful to reuse some already processed data from handler. | ||
*/ | ||
public interface LocalChannel { | ||
|
||
/** | ||
* Put some unique data in the channel | ||
* | ||
* @param identifier the identifier (key) of data | ||
* @param value the value to store | ||
* @return the allocated instance | ||
*/ | ||
<T> T allocated(String identifier, T value); | ||
|
||
/** | ||
* Put some data in the channel and override the previous value in case of duplicate | ||
* | ||
* @param identifier the identifier (key) of data | ||
* @param value the value to store | ||
* @param <T> type of data | ||
* @return the allocated instance | ||
*/ | ||
<T> T override(String identifier, T value); | ||
|
||
/** | ||
* Check if channel contains the given value | ||
* | ||
* @param type the type to search for | ||
* @return true if channel contains the requested data | ||
*/ | ||
boolean contains(Class<?> type); | ||
|
||
/** | ||
* Check if channel contains the given value | ||
* | ||
* @param element the identifier to search for | ||
* @return true if channel contains the requested data | ||
*/ | ||
boolean contains(String element); | ||
|
||
/** | ||
* Get data from the channel | ||
* | ||
* @param type type of value to get | ||
* @param <T> the type | ||
* @return the data | ||
*/ | ||
<T> T get(Class<T> type); | ||
|
||
/** | ||
* Get data from the channel | ||
* | ||
* @param identifier the identifier of value to get | ||
* @param <T> the type | ||
* @return the data | ||
*/ | ||
<T> T get(String identifier); | ||
|
||
/** | ||
* Get data from the channel | ||
* | ||
* @param identifier the identifier of value to get | ||
* @param type the class of the data | ||
* @param <T> the type | ||
* @return the data | ||
*/ | ||
<T> T get(String identifier, Class<T> type); | ||
|
||
/** | ||
* Get data container | ||
* | ||
* @return the map that contains channel data | ||
*/ | ||
Map<String, Object> getData(); | ||
|
||
} |
44 changes: 0 additions & 44 deletions
44
...rk/src/main/java/org/panda_lang/framework/design/interpreter/parser/pipeline/Channel.java
This file was deleted.
Oops, something went wrong.
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
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
40 changes: 0 additions & 40 deletions
40
...main/java/org/panda_lang/framework/language/interpreter/parser/pipeline/PandaChannel.java
This file was deleted.
Oops, something went wrong.
109 changes: 109 additions & 0 deletions
109
...java/org/panda_lang/framework/language/interpreter/parser/pipeline/PandaLocalChannel.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,109 @@ | ||
/* | ||
* Copyright (c) 2020 Dzikoysk | ||
* | ||
* 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 org.panda_lang.framework.language.interpreter.parser.pipeline; | ||
|
||
import org.panda_lang.framework.design.interpreter.parser.LocalChannel; | ||
import org.panda_lang.utilities.commons.ObjectUtils; | ||
import org.panda_lang.utilities.commons.function.Option; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
import java.util.NoSuchElementException; | ||
|
||
public final class PandaLocalChannel implements LocalChannel { | ||
|
||
private static final int DEFAULT_SIZE = 8; | ||
|
||
private final Map<String, Object> data; | ||
|
||
public PandaLocalChannel() { | ||
this.data = new HashMap<>(DEFAULT_SIZE); | ||
} | ||
|
||
public PandaLocalChannel(LocalChannel channel) { | ||
this.data = new HashMap<>(channel.getData().size() + DEFAULT_SIZE); | ||
this.data.putAll(channel.getData()); | ||
} | ||
|
||
@Override | ||
public <T> T allocated(String identifier, T value) { | ||
Object previous = data.put(identifier, value); | ||
|
||
if (previous != null) { | ||
throw new IllegalStateException("Duplicated identifier '" + identifier + "'"); | ||
} | ||
|
||
return value; | ||
} | ||
|
||
@Override | ||
public <T> T override(String identifier, T value) { | ||
data.put(identifier, value); | ||
return value; | ||
} | ||
|
||
@Override | ||
public boolean contains(Class<?> type) { | ||
for (Entry<String, Object> entry : data.entrySet()) { | ||
Object value = entry.getValue(); | ||
|
||
if (value != null && type.isAssignableFrom(value.getClass())) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
@Override | ||
public boolean contains(String identifier) { | ||
return data.containsKey(identifier); | ||
} | ||
|
||
@Override | ||
public <T> T get(Class<T> type) { | ||
for (Entry<String, Object> entry : data.entrySet()) { | ||
Object value = entry.getValue(); | ||
|
||
if (value != null && type.isAssignableFrom(value.getClass())) { | ||
return ObjectUtils.cast(value); | ||
} | ||
} | ||
|
||
throw new NoSuchElementException("Cannot find data matching " + type.getSimpleName() + " type in current channel"); | ||
} | ||
|
||
@Override | ||
public <T> T get(String identifier, Class<T> type) { | ||
return get(identifier); | ||
} | ||
|
||
@Override | ||
@SuppressWarnings("unchecked") | ||
public <T> T get(String identifier) { | ||
return (T) Option.of(data.get(identifier)).orThrow(() -> { | ||
throw new NoSuchElementException("'" + identifier + "' value not found in channel"); | ||
}); | ||
} | ||
|
||
@Override | ||
public Map<String, Object> getData() { | ||
return data; | ||
} | ||
|
||
} |
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
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
Oops, something went wrong.