java - Cache filter on collection with Guava -


i'm developing program performs operations on automatas. automata made of states (aka node) , transitions (aka edges) , need filter them retrieve set particular properties. operation easy implement, performed several times , write little cache on it.

in fragment of code below there implementation , i'd know if correct way filter , memoize observable transitions.

public class automata {     private state initial;     private set <state> states;      private set <transition> transitions;     private supplier <set <transition>> observables;      // ...      public automata() {         this.initial = new state();         this.states = new hashset <> ();         this.transitions = new hashset <> ();          this.observables = suppliers.memoize(() ->            transitions.stream().filter((t) ->                (t.isobservable() == true)).collect(collectors.toset()));     }      public getobservables() {          return observables.get();     } } 

questions:

  1. is correct?
  2. if transition changes observability information propagate supplier?

i'm sorry poor english, hope enough clear.

  1. yes, it's correct.
  2. no, changes in transitions won't automatically propagated. , case supplier afaik not suitable. either need overwrite manually here:

    public void invalidate(){     memorized = suppliers.memoize(supplier); } 

    also memoizewithexpiration work if know update not frequent , don't need reliable reads.

    or need use cache, example:

    cacheloader<key, graph> loader = new cacheloader<key, graph>() {   public graph load(key key) throws anyexception {     return createexpensivegraph(key);   } }; loadingcache<key, graph> cache = cachebuilder.newbuilder().build(loader); 

Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -