gw.lang.enhancements.CoreMapEnhancement.gsx Maven / Gradle / Ivy
package gw.lang.enhancements
uses java.util.*
uses java.io.*
uses gw.util.AutoMap
/*
* Copyright 2014 Guidewire Software, Inc.
*/
enhancement CoreMapEnhancement : java.util.Map
{
property get Keys() : java.util.Set {
return this.keySet()
}
property get Values() : java.util.Collection {
return this.values()
}
public function eachKeyAndValue( eachBlock(k : K, val : V):void ) : void
{
for( entry in this.entrySet() )
{
eachBlock( entry.Key, entry.Value )
}
}
public function eachKey( eachBlock(k : K):void ) : void
{
for( key in Keys )
{
eachBlock( key )
}
}
public function eachValue( eachBlock(value : V):void ) : void
{
for( key in Keys )
{
eachBlock( this[key] )
}
}
public function mapValues( mapper(value : V):V2 ) : java.util.Map
{
var returnMap = new HashMap()
eachKeyAndValue( \ k, v ->{ returnMap[k] = mapper(v) } )
return returnMap
}
reified function toAutoMap( defaultValue(k : K):V ) : Map {
return new AutoMap(this, defaultValue)
}
/**
* Returns a copy of this set
*/
function copy() : Map {
return new HashMap(this)
}
/**
* Removes all entries whose keys do not satisfy the keyFilter expression
* Return true if this map changed as a result of the call
*/
function retainWhereKeys( keyFilter(k : K) : boolean ) : boolean {
var modified = false
var it = this.entrySet().iterator()
while( it.hasNext() ) {
var entry = it.next()
if(not keyFilter( entry.Key ) ) {
it.remove()
modified = true
}
}
return modified
}
/**
* Removes all entries whose values do not satisfy the valueFilter expression
* Return true if this map changed as a result of the call
*/
function retainWhereValues( valueFilter(v : V) : boolean ) : boolean {
var modified = false
var it = this.entrySet().iterator()
while( it.hasNext() ) {
var entry = it.next()
if(not valueFilter( entry.Value ) ) {
it.remove()
modified = true
}
}
return modified
}
/**
* Removes all entries whose keys do not satisfy the keyFilter expression
* and returns the new resulting map
*/
function filterByKeys( keyFilter(k : K):boolean ) : Map {
var returnMap = new HashMap()
eachKeyAndValue( \ k, v ->{ if( keyFilter( k ) ) { returnMap[k] = v } } )
return returnMap
}
/**
* Removes all entries whose values do not satisfy the valueFilter expression
* and returns the new resulting map
*/
function filterByValues( valueFilter(v : V):boolean ) : Map {
var returnMap = new HashMap()
eachKeyAndValue( \ k, v ->{ if( valueFilter( v ) ) { returnMap[k] = v } } )
return returnMap
}
/**
* Uses a java.util.Properties object to write the contents of this
* map to the given file in the java properties format.
*/
function writeToPropertiesFile( file : File ) {
writeToPropertiesFile( file, "" )
}
/**
* Uses a java.util.Properties object to write the contents of this
* map to the given file in the java properties format.
*/
function writeToPropertiesFile( file : File, comments : String ) {
var x = new Properties()
x.putAll( this )
using( var fos = new FileOutputStream( file ) ) {
x.store( fos, comments )
}
}
/**
* Reads the given properties file into a map
*/
static function readFromPropertiesFile( file : File ) : Map {
if( not file.exists() ) {
return {}
}
using( var ins = new FileInputStream( file ) )
{
var x = new Properties()
x.load( ins )
return x as Map
}
}
property get Count() : int {
return this.size()
}
}