You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by "Mark R. Diggory" <md...@latte.harvard.edu> on 2004/03/04 18:05:38 UTC

[collections] Yipes, I need Regexp based get on a Map

I'm trying to put together a lookup mechanism that is regular expression 
based. For example


Map map = ...;

Bar bar = new Bar();

map.put("^http://foo.bar*",bar);


Bar bar2 = (Bar)map.get("http://foo.bar/bam");


get("...") would return the bar object. Of course, this could match 
multiple values, as such get("...") could return the first value 
encountered or a Collection of all the matches encountered.

Collection bars = (Collection)map.get("http://foo.bar/bam");
Iterator iter = bars.iterator();
Bar bar2 = (Bar)iter.next();

Any ideas on how I can throw this together using Commons Collections as 
a basis? I can live with just >=j2sdk1.4 compatibility. Is this 
something Commons Collections would like to have available?

-Mark
-- 
Mark Diggory
Software Developer
Harvard MIT Data Center
http://www.hmdc.harvard.edu

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Re: [collections] Yipes, I need Regexp based get on a Map

Posted by "Mark R. Diggory" <md...@latte.harvard.edu>.
This may be the simplest and best solution to meet my needs, I hadn't 
been thinking of the Predicate Functor style approach. Thanks for the 
suggestion. I think this is the wise choice because the underlying 
Collection interface behavior is not redefined in terms of its behavior 
in the map. The struggle with the whole regexp based map key thing is 
getting an efficient lookup strategy, which isn't very plausible if you 
can't reproduce the regexp from the string used as the key in the 
get(Object key) method, I still need to evaluate each and every key for 
matches. These maps will not be large, and a Predicate that matches all 
the keys that fit the criteria is an elegant approach.

-Mark

Stephen Colebourne wrote:
> What about writing a Predicate for your regex.
> Then use CollectionsUtils.filter(map.keySet(), predicate) to get the
> matches.
> 
> For alternate solutions search the mailing list for 'Trie'. IIRC it is meant
> to handle text prefix searches (or look at Lucene). Of course neither handle
> regex. I don't think that a regex dependency for [collections] is good
> however.
> 
> Stephen
> 
> 
> ----- Original Message -----
> From: "Mark R. Diggory" <md...@latte.harvard.edu>
> 
>>I'm trying to put together a lookup mechanism that is regular expression
>>based. For example
>>
>>
>>Map map = ...;
>>
>>Bar bar = new Bar();
>>
>>map.put("^http://foo.bar*",bar);
>>
>>
>>Bar bar2 = (Bar)map.get("http://foo.bar/bam");
>>
>>
>>get("...") would return the bar object. Of course, this could match
>>multiple values, as such get("...") could return the first value
>>encountered or a Collection of all the matches encountered.
>>
>>Collection bars = (Collection)map.get("http://foo.bar/bam");
>>Iterator iter = bars.iterator();
>>Bar bar2 = (Bar)iter.next();
>>
>>Any ideas on how I can throw this together using Commons Collections as
>>a basis? I can live with just >=j2sdk1.4 compatibility. Is this
>>something Commons Collections would like to have available?
>>
>>-Mark
>>--
>>Mark Diggory
>>Software Developer
>>Harvard MIT Data Center
>>http://www.hmdc.harvard.edu
>>
>>---------------------------------------------------------------------
>>To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
>>For additional commands, e-mail: commons-dev-help@jakarta.apache.org
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-dev-help@jakarta.apache.org
> 

-- 
Mark Diggory
Software Developer
Harvard MIT Data Center
http://www.hmdc.harvard.edu

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Re: [collections] Yipes, I need Regexp based get on a Map

Posted by Stephen Colebourne <sc...@btopenworld.com>.
What about writing a Predicate for your regex.
Then use CollectionsUtils.filter(map.keySet(), predicate) to get the
matches.

For alternate solutions search the mailing list for 'Trie'. IIRC it is meant
to handle text prefix searches (or look at Lucene). Of course neither handle
regex. I don't think that a regex dependency for [collections] is good
however.

Stephen


----- Original Message -----
From: "Mark R. Diggory" <md...@latte.harvard.edu>
> I'm trying to put together a lookup mechanism that is regular expression
> based. For example
>
>
> Map map = ...;
>
> Bar bar = new Bar();
>
> map.put("^http://foo.bar*",bar);
>
>
> Bar bar2 = (Bar)map.get("http://foo.bar/bam");
>
>
> get("...") would return the bar object. Of course, this could match
> multiple values, as such get("...") could return the first value
> encountered or a Collection of all the matches encountered.
>
> Collection bars = (Collection)map.get("http://foo.bar/bam");
> Iterator iter = bars.iterator();
> Bar bar2 = (Bar)iter.next();
>
> Any ideas on how I can throw this together using Commons Collections as
> a basis? I can live with just >=j2sdk1.4 compatibility. Is this
> something Commons Collections would like to have available?
>
> -Mark
> --
> Mark Diggory
> Software Developer
> Harvard MIT Data Center
> http://www.hmdc.harvard.edu
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-dev-help@jakarta.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Re: [collections] Yipes, I need Regexp based get on a Map

Posted by Phil Steitz <ph...@steitz.com>.
Mark R. Diggory wrote:
> I'm trying to put together a lookup mechanism that is regular expression 
> based. For example
> 
> 
> Map map = ...;
> 
> Bar bar = new Bar();
> 
> map.put("^http://foo.bar*",bar);
> 
> 
> Bar bar2 = (Bar)map.get("http://foo.bar/bam");
> 
> 
> get("...") would return the bar object. Of course, this could match 
> multiple values, as such get("...") could return the first value 
> encountered or a Collection of all the matches encountered.
> 
> Collection bars = (Collection)map.get("http://foo.bar/bam");
> Iterator iter = bars.iterator();
> Bar bar2 = (Bar)iter.next();
> 
> Any ideas on how I can throw this together using Commons Collections as 
> a basis? I can live with just >=j2sdk1.4 compatibility. Is this 
> something Commons Collections would like to have available?
> 
> -Mark

Yet another suggestion: have a look at o.a.c.map.AbstractHashedMap.  By 
extending this, you can do pretty much anything you want to the get/put 
semantics of a map.

Phil


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org