Git Product home page Git Product logo

path's Introduction

!> 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 library.

Getting Started

Add the following dependency to your maven project. Gradle and others are also supported.

<dependency>
  <groupId>io.devnet.util</groupId>
  <artifactId>util-path</artifactId>
  <version>0.5.0</version>
</dependency>

Basic Usage

Using Methods

Resolve the property path using getters

String path = Path.for(Album.class)
  .$(Album::getArtist)
  .$(Artist::getGenre)
  .$(Genre::getName)
  .resolve();
  
System.out.println(path); genre

Shorthand syntax (max depth 5)

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

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)

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

Path path = Path.for(Album.class).$(Album::getArtist).$(Artist::getGenere);

Return a string representation of the chain

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.

Method method = path.lastMethod(); // getGenere()

Return the type of the last field in the chain

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.

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.

String departmentName = "accounting";

// shorthand

SearchResult<Employee> result = searchService.find(Employee.class)
  .eq(Employee::getDepartment, Department::getName, departmentName)
  .find();
 
// criteria approach
 
SearchCriteria<Employee> criteriaOne = SearchCriteria.for(Employee.class)
    .$(Employee::getDeplartment)
    .$(Department::getName)
    .eq(departmentName);

// or
SearchCriteria<Employee> criteriaTwo = SearchCriteria.for(Employee.class)
    .eq(Employee::getDeplartment, Department::getName, departmentName);
    
SearchResult<Employee> 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.

String departmentName = "accounting";

// shorthand

SearchResult<Employee> result = searchService.find(Employee.class)
  .eq("department.name", departmentName) // method params String, Object
  .find();
 
// criteria approach

SearchCriteria<Employee> criteriaTwo = SearchCriteria.for(Employee.class)
    .eq("department.name", departmentName); // method params: String, Object
    
SearchResult<Employee> result = searchService.find(criteriaOne);

Implementation

Path provides a convient syntax on top of Jodd's MethodRef for chaining multiple references together. The method are never invoked.

path's People

Contributors

devnet-io avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.