001package Torello.Java; 002 003import java.util.function.Consumer; 004import java.util.function.Supplier; 005 006/** 007 * Provides any method the ability to provide its users the ability to choose what kind of data 008 * structure should be used for returning the method's results. Currently, this class is used as 009 * a helper class for class {@link FileNode} 010 * 011 * @param <INSERT_TYPE> Currently, the only class using {@code VarType} is class {@link FileNode}. 012 * Within class {@link FileNode}, this Type-Parameter, {@code 'INSERT_TYPE'}, is always assigned 013 * the value of class {@code 'FileNode'} 014 * 015 * @param <RET_DATA_STRUCTURE> This Generic Type-Parameter provides a user the ability to specify 016 * any Method Return-Type needed / desired. In class {@link FileNode}, for instance, this 017 * Type-Parameter is often assigned to things such as: {@code Stream<FileNode>}, 018 * {@code Iterator<FileNode>} or even {@code String[]} - where each of the {@code String's} are 019 * actual File-Names as a {@code String}. 020 * 021 * <BR /><BR />If this class {@code VarType} where used elsewhere, outside of {@code FileNode}, 022 * then the Generic Type-Parameter {@code 'RET_DATA_STRUCTURE'} would allow a user to provide a 023 * means for his / her users to specify, exactly, any method's Return Type/Class. 024 * 025 * @see FileNode 026 */ 027public class VarType<RET_DATA_STRUCTURE, INSERT_TYPE> 028{ 029 /** Accepts and inserts data into the data-structure. */ 030 public final Consumer<INSERT_TYPE> inserter; 031 032 /** Performs any finalizations on the data-structure, and then returns it */ 033 public final Supplier<RET_DATA_STRUCTURE> finisher; 034 035 // Identifies whether this instance has already been run to completion 036 private boolean finished = false; 037 038 /** Identifies whether this is a "Reusable" instance */ 039 public final boolean isReusable; 040 041 /** 042 * Constructs an instance of this class 043 * @param inserter This is assigned to the field {@link #inserter}. 044 * @param finisher Assigned to field {@link #finisher}. 045 */ 046 public VarType( 047 final Consumer<INSERT_TYPE> inserter, 048 final Supplier<RET_DATA_STRUCTURE> finisher, 049 final boolean isReusable 050 ) 051 { 052 if (isReusable) 053 { 054 this.isReusable = true; 055 this.inserter = inserter; 056 this.finisher = finisher; 057 } 058 059 else 060 { 061 this.isReusable = false; 062 063 this.inserter = (INSERT_TYPE data) -> 064 { 065 if (this.finished) throw new VarTypeAlreadyUsedException(); 066 inserter.accept(data); 067 }; 068 069 this.finisher = () -> 070 { 071 if (this.finished) throw new VarTypeAlreadyUsedException(); 072 this.finished = true; 073 return finisher.get(); 074 }; 075 } 076 } 077}