https://github.com/devnet-io/path
Simple java utility for providing strongly typed references to nested methods and fields
https://github.com/devnet-io/path
criteria java strongly-typed util
Last synced: 9 months ago
JSON representation
Simple java utility for providing strongly typed references to nested methods and fields
- Host: GitHub
- URL: https://github.com/devnet-io/path
- Owner: devnet-io
- Created: 2020-01-14T09:29:13.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2021-05-24T07:17:57.000Z (about 5 years ago)
- Last Synced: 2025-04-03T20:41:28.560Z (over 1 year ago)
- Topics: criteria, java, strongly-typed, util
- Language: Java
- Homepage: https://www.devnet.io/libs/path
- Size: 21.5 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
!> Note: this library is under active development, breaking changes may occur
# path
Path is a simple java utility for providing strongly typed references to nested methods and fields.
It uses a convienent java 8 syntax for reflection, evaluation, and assertion.
Path builds on Jodd's excellent (and very fast) [Proxetta](https://jodd.org/proxetta/) library.
- [Getting Started](#getting-started)
- [Basic Usage](#basic-usage)
- [With Methods](#using-methods)
- [With Fields](#using-fields)
- [Output](#output)
- [Use Cases](#use-cases)
- [Strongly Typed Reflection](#strongly-typed-reflection)
- [Validation](#validation)
- [Database Queries](#strongly-typed-database-queries-using-criteria)
- [Implementation](#implementation)
- [Licensing](#licensing)
## Getting Started
Add the following dependency to your maven project. Gradle and others are also supported.
```xml
io.devnet.util
util-path
0.5.0
```
## Basic Usage
#### Using Methods
Resolve the property path using getters
```java
String path = Path.for(Album.class)
.$(Album::getArtist)
.$(Artist::getGenre)
.$(Genre::getName)
.resolve();
System.out.println(path); genre
```
Shorthand syntax (max depth 5)
```java
String path = Path.for(Album::getArtist, Artist::getGenre, Genre::getName).resolve();
System.out.println(path); // artist.genere.name
```
#### Using Fields
Resolve property path using fields
```java
String path = Path.for(Album.class)
.$(album -> album.artist)
.$(artist -> artist.genre)
.$(genre -> genre.name)
.resolve();
System.out.println(path); // artist.genere.name
```
Shorthand syntax (max depth 5)
```java
String path = Path.for(a -> a.artist, a -> a.genre, g -> g.name).resolve();
System.out.println(path); // artist.genere.name
```
### Output
Create the path chain
```java
Path path = Path.for(Album.class).$(Album::getArtist).$(Artist::getGenere);
```
Return a string representation of the chain
```java
String fieldPath = path.resolve(); // "artist.genere.name"
// include the parent in the path
String fullPath = path.resolve(Path.RESOLVE_PARENT); // "album.artist.genre.name"
```
Return a java.lang.reflect object for the last method in the chain. Throws MethodNotFound exception if the chain is built with fields.
```java
Method method = path.lastMethod(); // getGenere()
```
Return the type of the last field in the chain
```java
Class> field = path.lastField(); // String.class, return type of getGenere()
```
Retrieve the value for the last field in the chain by passing an instance of the parent class. The getters will be invoked if they are present in the chain.
```java
Album album = myService.getAlbum();
String value = path.evaluate(album); // "synthwave", return of the last method in the chain: getGenere()
```
## Use cases
### Strongly typed reflection
### Validation
### Strongly typed database queries using criteria
This syntax allows implementations to both enforce the paths used in joins are correct and ensure that the input passed to the criteria is of the right type.
In this example Employee must have a method named getDepartment, department must have a method named getName, and
departmentName of the same type as Department$getName's return type.
```java
String departmentName = "accounting";
// shorthand
SearchResult result = searchService.find(Employee.class)
.eq(Employee::getDepartment, Department::getName, departmentName)
.find();
// criteria approach
SearchCriteria criteriaOne = SearchCriteria.for(Employee.class)
.$(Employee::getDeplartment)
.$(Department::getName)
.eq(departmentName);
// or
SearchCriteria criteriaTwo = SearchCriteria.for(Employee.class)
.eq(Employee::getDeplartment, Department::getName, departmentName);
SearchResult result = searchService.find(criteriaOne)
```
A classic implementation may appear as follows. Here we do not know if the entities fields are and input types are valid until runtime.
```java
String departmentName = "accounting";
// shorthand
SearchResult result = searchService.find(Employee.class)
.eq("department.name", departmentName) // method params String, Object
.find();
// criteria approach
SearchCriteria criteriaTwo = SearchCriteria.for(Employee.class)
.eq("department.name", departmentName); // method params: String, Object
SearchResult result = searchService.find(criteriaOne);
```
## Implementation
Path provides a convient syntax on top of [Jodd's MethodRef](https://jodd.org/ref/methref.html) for chaining multiple references together. The method are never invoked.