Skip to content

Commit

Permalink
GH-527 Remove InterceptorData and LocalCache and delegate its functio…
Browse files Browse the repository at this point in the history
…nalities to LocalChannel data
  • Loading branch information
dzikoysk committed Jul 5, 2020
1 parent 1a9c93c commit 0aaae53
Show file tree
Hide file tree
Showing 64 changed files with 511 additions and 519 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.panda_lang.framework.design.interpreter.Interpretation;
import org.panda_lang.framework.design.interpreter.parser.expression.ExpressionParser;
import org.panda_lang.framework.design.interpreter.parser.generation.Generation;
import org.panda_lang.framework.design.interpreter.parser.pipeline.Channel;
import org.panda_lang.framework.design.interpreter.parser.pipeline.PipelinePath;
import org.panda_lang.framework.design.interpreter.source.SourceSet;
import org.panda_lang.framework.design.interpreter.token.Snippet;
Expand Down Expand Up @@ -110,7 +109,7 @@ public final class Components {
/**
* Represents the channel between handler and parser
*/
public static final ContextComponent<Channel> CHANNEL = ContextComponent.of("channel", Channel.class);
public static final ContextComponent<LocalChannel> CHANNEL = ContextComponent.of("channel", LocalChannel.class);

/**
* Represents the current scope
Expand Down
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();

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import org.jetbrains.annotations.Nullable;
import org.panda_lang.framework.design.interpreter.parser.Context;
import org.panda_lang.framework.design.interpreter.parser.LocalChannel;
import org.panda_lang.framework.design.interpreter.token.Snippet;

/**
Expand All @@ -38,6 +39,6 @@ public interface Handler {
* @param source source
* @return the result object
*/
@Nullable Object handle(Context context, Channel channel, Snippet source);
@Nullable Object handle(Context context, LocalChannel channel, Snippet source);

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.panda_lang.framework.design.interpreter.parser.pipeline;

import org.panda_lang.framework.design.interpreter.parser.Context;
import org.panda_lang.framework.design.interpreter.parser.LocalChannel;
import org.panda_lang.framework.design.interpreter.parser.Parser;
import org.panda_lang.framework.design.interpreter.parser.ParserRepresentation;
import org.panda_lang.framework.design.interpreter.token.Snippet;
Expand All @@ -33,7 +34,7 @@ public interface Pipeline<P extends Parser> {
* @param source the source
* @return parser which fits to the source
*/
HandleResult<P> handle(Context context, Channel channel, Snippet source);
HandleResult<P> handle(Context context, LocalChannel channel, Snippet source);

/**
* Register the specified parser to
Expand Down

This file was deleted.

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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.panda_lang.framework.design.interpreter.parser.Context;
import org.panda_lang.framework.design.interpreter.parser.Parser;
import org.panda_lang.framework.design.interpreter.parser.ParserRepresentation;
import org.panda_lang.framework.design.interpreter.parser.pipeline.Channel;
import org.panda_lang.framework.design.interpreter.parser.LocalChannel;
import org.panda_lang.framework.design.interpreter.parser.pipeline.HandleResult;
import org.panda_lang.framework.design.interpreter.parser.pipeline.Handler;
import org.panda_lang.framework.design.interpreter.parser.pipeline.Pipeline;
Expand Down Expand Up @@ -53,7 +53,7 @@ public PandaPipeline(Pipeline<P> parentPipeline, String name) {
}

@Override
public HandleResult<P> handle(Context context, Channel channel, Snippet source) {
public HandleResult<P> handle(Context context, LocalChannel channel, Snippet source) {
if (count > 1000) {
count = 0;
sort();
Expand All @@ -70,7 +70,7 @@ public HandleResult<P> handle(Context context, Channel channel, Snippet source)
return handle(context, channel, source, representations);
}

private HandleResult<P> handle(Context context, Channel channel, Snippet source, Collection<? extends ParserRepresentation<P>> representations) {
private HandleResult<P> handle(Context context, LocalChannel channel, Snippet source, Collection<? extends ParserRepresentation<P>> representations) {
long currentTime = System.nanoTime();
InterpreterFailure failure = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ void getPipeline() {
@Test
void getTotalHandleTime() {
Assertions.assertNull(defaultPath.getPipeline(TEST_COMPONENT)
.handle(new PandaContext(), new PandaChannel(), PandaTokenInfo.of(TokenTypes.UNKNOWN, "test").toSnippet())
.handle(new PandaContext(), new PandaLocalChannel(), PandaTokenInfo.of(TokenTypes.UNKNOWN, "test").toSnippet())
.getParser().getOrNull());

Assertions.assertTrue(defaultPath.getTotalHandleTime() > 0);
Expand Down
Loading

0 comments on commit 0aaae53

Please sign in to comment.