All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nyla.solutions.global.patterns.search.ReLookup Maven / Gradle / Ivy

Go to download

Nyla Solutions Global Java API provides support for basic application utilities (application configuration, data encryption, debugger and text processing).

The newest version!
package nyla.solutions.global.patterns.search;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import nyla.solutions.global.util.Text;

/**
 * 
 * 
 * The RELookup acts as a lookup table where the key is a complex 
 * regular expression. The expression and values are stored in a hash map.
 * Then RELookup.get(argument) method is called,
 * RELookup operation will iterate through the given get argument expressions
 * looking for a match on the corresponding complex regular expression key. 
 * 
 * The value column of the lookup table is used if the regular expression matches 
 * the argument.
 *  
 * v
 * 
 * 		ReLookup lookup = new ReLookup();
		
		Assert.assertTrue(lookup instanceof Map);
		
		lookup.put("(001)*.*Green.*${AND}${NOT}.*Blue.*", new FaultError("0001","ERROR"));
		lookup.put("(002)*.*Green.*${AND}.*Blue.*", new FaultError("0002","GB"));
		lookup.put("(003)*.*Blue.*", new FaultError("0003","BLUE"));
		
		Assert.assertEquals(lookup.get("Green").getCode(), "0001");
		Assert.assertEquals(lookup.get("Blue Green").getCode(), "0002");
		Assert.assertEquals(lookup.get("Blue with Live of pure").getCode(), "0003");
   
   

Complex Regular Expression (And/Not)

By default, regular expressions do not have an easy way to chain expressions together using AND/NOT logic. The OR logical expression is supported with the character "|". The RELookup operation combines regular expressions with a special syntax to support AND/NOT logic.

AND Operation

The RELookup supports chaining expressions together with "AND" logic. This is accomplished by chaining expressions together with “${AND}”. The string “"{AND}" can be used to separate two regular expressions. If any of the regular expressions return false then the entire regular expression is false. In the following example, the regular expression “.*USA.*${AND}.*Greece.*”, only returns true if the text contains both “USA” and “Greece”.
Complex REValue Matches
.*USA.*${AND}.*Greece.* USA and GreeceTrue
.*USA.*${AND}.*Greece.* USAFalse
.*USA.*${AND}.*Greece.* GreeceFalse
.*USA.*${AND}.*Greece.* Greece USA True

NOT Operation

The RELookup supports negative logic (NOT) for expressions. This is accomplished by prefixing the expressions with “${NOT}”. In the following example, the regular expression “.*USA.*” only returns true if the text does not contain the word “USA”. Note that multiple “&{NOT}”(s) can be chained together with “${AND}”(s) (see table below).
Complex REValueMatches
${NOT}.*USA.* USA and Greece False
${NOT}.*USA.* USA False
${NOT}.*USA.* Greece True
${NOT}.*USA.* Greece USA False
.*Greece.*${AND}${NOT}.*USA.* ${AND}${NOT}.*Turkey.* Greece Turkey False
.*Greece.*${AND}${NOT}.*USA.* ${AND}${NOT}.*Turkey.* Greece Africa True
* @author Gregory Green * */ public class ReLookup implements Map { /** * * Default constructor */ public ReLookup() { }// --------------------------- /** // / Lookup a value of the dictionary // / // / the value to compare the keys @return the match value for the key */ public T lookup(String text) { if (text == null) return null; for (Entry entry :lookupMap.entrySet()) { if (Text.matches(text, entry.getKey())) return lookupMap.get(entry.getKey()); } return null; }// ------------------------------ /** * * @param reExpression the Regular expression * @return collection of values the match text RE expression */ public Collection lookupCollection(String reExpression) { if (reExpression == null) return null; ArrayList collection = new ArrayList(lookupMap.size()); for (Entry entry :lookupMap.entrySet()) { if (Text.matches(reExpression, entry.getKey())) collection.add(lookupMap.get(entry.getKey())); } collection.trimToSize(); return collection; }// ------------------------------ /** * Add a lookup item * keySet() { return lookupMap.keySet(); } /** * @return * @see java.util.Map#entrySet() */ public Set> entrySet() { return lookupMap.entrySet(); } /** * @param obj * @return * @see java.util.Map#equals(java.lang.Object) */ public boolean equals(Object obj) { return lookupMap.equals(obj); } /** * @return * @see java.util.Map#hashCode() */ public int hashCode() { return lookupMap.hashCode(); } /** * @return * @see java.util.Map#size() */ public int size() { return lookupMap.size(); }// -------------------------------------------------------- /** * @param obj * @return * @see java.util.Map#remove(java.lang.Object) */ public T remove(Object obj) { return lookupMap.remove(obj); }// -------------------------------------------------------- /** * @param map * @see java.util.Map#putAll(java.util.Map) */ public void putAll(Map map) { lookupMap.putAll(map); }// -------------------------------------------------------- /** * @return * @see java.util.Map#values() */ public Collection values() { return lookupMap.values(); }// -------------------------------------------------------- private Map lookupMap = new TreeMap(); }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy