-
Notifications
You must be signed in to change notification settings - Fork 9
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
Introduce OneToManyValueExpression and OneToOneValueExpression #247
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
77 changes: 77 additions & 0 deletions
77
core/src/main/java/io/parsingdata/metal/expression/value/OneToOneValueExpression.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,77 @@ | ||
/* | ||
* Copyright 2013-2016 Netherlands Forensic Institute | ||
* | ||
* 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 io.parsingdata.metal.expression.value; | ||
|
||
import static io.parsingdata.metal.Util.checkNotNull; | ||
|
||
import java.util.Objects; | ||
import java.util.Optional; | ||
|
||
import io.parsingdata.metal.Util; | ||
import io.parsingdata.metal.data.ImmutableList; | ||
import io.parsingdata.metal.data.ParseState; | ||
import io.parsingdata.metal.encoding.Encoding; | ||
|
||
/** | ||
* Base class for {@link ValueExpression}s with one operand that evaluates | ||
* to a single value. | ||
* <p> | ||
* A OneToOneValueExpression implements a ValueExpression that has one | ||
* <code>operand</code> (a {@link ValueExpression}). The operand is first | ||
* evaluated. If it evaluates to {@link Optional#empty()}, the result of the | ||
* ValueExpression itself will be that as well. | ||
* <p> | ||
* To implement a OneToOneValueExpression, only the | ||
* {@link #eval(ImmutableList, ParseState, Encoding)} must | ||
* be implemented, handling the case of evaluating a list of values. This | ||
* base class takes care of evaluating the operand and handling list | ||
* semantics. | ||
* | ||
* @see BinaryValueExpression | ||
*/ | ||
public abstract class OneToOneValueExpression implements ValueExpression { | ||
|
||
public final ValueExpression operand; | ||
|
||
public OneToOneValueExpression(final ValueExpression operand) { | ||
this.operand = checkNotNull(operand, "operand"); | ||
} | ||
|
||
@Override | ||
public ImmutableList<Optional<Value>> eval(final ParseState parseState, final Encoding encoding) { | ||
return ImmutableList.create(eval(operand.eval(parseState, encoding), parseState, encoding)); | ||
} | ||
|
||
public abstract Optional<Value> eval(final ImmutableList<Optional<Value>> list, final ParseState parseState, final Encoding encoding); | ||
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. Self-review: I think we do not need to pass 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. |
||
|
||
@Override | ||
public String toString() { | ||
return getClass().getSimpleName() + "(" + operand + ")"; | ||
} | ||
|
||
@Override | ||
public boolean equals(final Object obj) { | ||
return Util.notNullAndSameClass(this, obj) | ||
&& Objects.equals(operand, ((OneToOneValueExpression)obj).operand); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(getClass(), operand); | ||
} | ||
|
||
} |
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
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Referring to your naming debate, reading this does make it seem a bit funny in that it now reads like
negate/not
have a different cardinality between input and output (consider an identity function, it would be even more weird in my opinion), although both are simply (unbounded)ValueExpressions
. Before,Unary
meant a single operand, with implicitly a single output value (as always). Now I think it is a mix of two concepts.But thinking of a good name is hard.
UnaryManyToMany
to me feels like I would know better what it all is about. But there might be better names to be found. Or I am just thinking about it wrong. 🆘EDIT: as another example,
last
being aOneToOne
just does not quite feel right to me (many to one?).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.
I agree that because of our list semantics, the notion of what is a single operand is a bit complex... We should probably approach this in a more fundamental way: think about the possible types, then the ones that actually occur, then which ones we should implement/support and after all that, how to name them.
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.
Sounds good :) It is also interesting for expressions with more than one operand, where e.g.
mul
can have two lists, butexp
can have a list as the first operand, but requires a single element as the second.It would probably be cool to encode that information in the operand types, i.e.
BinaryExpr&ToManyExpr exp(ToManyExpr value, ToOneExpr second)
. It can then allow us to statically check for correctness as well, as per #70 (although there are few cases where it is necessary?). Downside is that we can not pass a list expression where we know that it will only ever contain a single value, so in theexp
case, it would need a wrap with alast
for example.And there are probably other things to consider.