public boolean hasPrefix( String key ) { return ( this.get( key ) != null ); }
public String getCommonPrefix() { StringBuffer buffer = new StringBuffer(); if( children.size() != 1 ) return buffer.toString(); buildPrefix( buffer, this ); return buffer.toString(); }
@Test public void testGetPrefix() { Trie<String> trie = new Trie<>(); trie.put( "a", "a" ); trie.put( "aa", "aa" ); trie.put( "aaa", "aaa" ); trie.put( "ab", "ab" ); trie.put( "abb", "abb" ); trie.put( "aabb", "aabb" ); assertEquals( null, trie.get( "" ) ); assertEquals( "a", trie.get( "a" ) ); assertEquals( "a", trie.get( "azzz" ) ); assertEquals( "aa", trie.get( "aa" ) ); assertEquals( "aaa", trie.get( "aaa" ) ); assertEquals( "ab", trie.get( "ab" ) ); assertEquals( "abb", trie.get( "abb" ) ); assertEquals( "aabb", trie.get( "aabb" ) ); assertEquals( "aabb", trie.get( "aabbcdef" ) ); assertNull( trie.get( "zzz" ) ); assertNull( trie.get( "" ) ); }
@Test public void testHasPrefix() { Trie<String> trie = new Trie<>(); trie.put( "a", "a" ); trie.put( "aa", "aa" ); trie.put( "aaa", "aaa" ); trie.put( "ab", "ab" ); trie.put( "abb", "abb" ); trie.put( "aabb", "aabb" ); assertTrue( trie.hasPrefix( "a" ) ); assertTrue( trie.hasPrefix( "aa" ) ); assertTrue( trie.hasPrefix( "aaa" ) ); assertFalse( trie.hasPrefix( "" ) ); assertFalse( trie.hasPrefix( "5" ) ); assertFalse( trie.hasPrefix( "zzzz" ) ); }
public void put( String key, V value ) { put( new StringBuffer( key ), new StringBuffer( "" ), value ); }
protected String getTapsCommonPrefix( FlowProcess<? extends Config> flowProcess ) { if( commonPrefix == null ) commonPrefix = getTapPrefixMap( flowProcess ).getCommonPrefix(); return commonPrefix; }
void put( StringBuffer remainder, StringBuffer prefix, V value ) { if( remainder.length() <= 0 ) { this.entry.value = value; this.entry.prefix = prefix.toString(); return; } char keyElement = remainder.charAt( 0 ); Trie<V> trie = null; try { trie = children.get( keyElement ); } catch( IndexOutOfBoundsException ignored ) { } if( trie == null ) { trie = new Trie<>( keyElement ); children.put( keyElement, trie ); } prefix.append( remainder.charAt( 0 ) ); trie.put( remainder.deleteCharAt( 0 ), prefix, value ); }
public V get( String key ) { return get( new StringBuffer( key ), 0 ); }
private void buildPrefix( StringBuffer buffer, Trie<V> current ) { if( current.children.size() != 1 ) return; for( Map.Entry<Character, Trie<V>> entry : current.children.entrySet() ) { buffer.append( entry.getKey() ); buildPrefix( buffer, entry.getValue() ); } }
V get( StringBuffer key, int level ) { if( key.length() <= 0 ) return entry.value; Trie<V> trie = children.get( key.charAt( 0 ) ); if( trie != null ) return trie.get( key.deleteCharAt( 0 ), ++level ); else return ( level > 0 ) ? entry.value : null; }
protected Child findMatchingTap( FlowProcess<? extends Config> flowProcess ) { // todo: in Cascading4, this value can be pulled from the FlowProcessContext String identifier = flowProcess.getStringProperty( "cascading.source.path" ); // we cannot determine the actual file being processed -- this was the default until 3.3 if( identifier == null ) return taps[ 0 ]; Trie<Child> prefixMap = getTapPrefixMap( flowProcess ); int index = identifier.indexOf( getTapsCommonPrefix( flowProcess ) ); // the child taps represent relative paths that cannot be resolved -- eg. globs if( index == -1 ) return taps[ 0 ]; Child child = prefixMap.get( identifier.substring( index ) ); if( child == null ) throw new IllegalStateException( "unable to find child having a prefix that matches: " + identifier ); return child; }