Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Criteria. Make Path a concrete class and enrich with fluent methods
- Loading branch information
1 parent
9fe85f9
commit baba31e
Showing
7 changed files
with
63 additions
and
27 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
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
55 changes: 52 additions & 3 deletions
55
criteria/common/src/org/immutables/criteria/constraints/Path.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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,10 +1,59 @@ | |||
package org.immutables.criteria.constraints; | package org.immutables.criteria.constraints; | ||
|
|
||
import com.google.common.base.Preconditions; | |||
import com.google.common.collect.ImmutableList; | |||
|
|||
import javax.annotation.Nullable; | |||
import java.util.Collections; | |||
import java.util.List; | |||
import java.util.Objects; | |||
|
|||
/** | /** | ||
* Access to a field. | * Access to a property possibly via several paths like {@code foo.bar.qux} | ||
*/ | */ | ||
public interface Path extends Expression { | public final class Path implements Expression { | ||
|
|||
private final ImmutableList<String> paths; | |||
|
|||
private Path(List<String> paths) { | |||
this.paths = ImmutableList.copyOf(paths); | |||
} | |||
|
|||
public List<String> paths() { | |||
return paths; | |||
} | |||
|
|||
public Path add(String path) { | |||
Objects.requireNonNull(path, "path"); | |||
Preconditions.checkArgument(!path.isEmpty(), "empty path"); | |||
return new Path(ImmutableList.<String>builder().addAll(paths).add(path).build()); | |||
} | |||
|
|||
public Path concat(Path other) { | |||
Objects.requireNonNull(other, "other"); | |||
return new Path(ImmutableList.<String>builder().addAll(this.paths).addAll(other.paths).build()); | |||
} | |||
|
|||
public static Path of(String path) { | |||
Objects.requireNonNull(path); | |||
return new Path(Collections.singletonList(path)); | |||
} | |||
|
|||
/** | |||
* Returns current path in java bean format: {@code foo.bar.qux} | |||
*/ | |||
public String toStringPath() { | |||
return String.join(".", paths()); | |||
} | |||
|
|
||
String path(); | @Override | ||
public String toString() { | |||
return toStringPath(); | |||
} | |||
|
|
||
@Nullable | |||
@Override | |||
public <R, C> R accept(ExpressionBiVisitor<R, C> visitor, @Nullable C context) { | |||
return visitor.visit(this, context); | |||
} | |||
} | } |
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