Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

78
LINES

< > BotCompany Repo | #1032783 // ConceptFieldIndexBase_certainValues

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (18627L/114K).

asclass ConceptFieldIndexBase_certainValues<A extends Concept, Val> implements IConceptIndex, IPartialFieldIndex<A, Val> {
  Concepts concepts;
  Class<A> cc;
  S field;
  Map<A, Val> objectToValue = syncHashMap();
  MultiSetMap<Val, A> valueToObject; // initialized in subclass

  *() { init(); }
  *(Class<A> cc, S field) { this(db_mainConcepts(), cc, field); }
  *(Concepts *concepts, Class<A> *cc, S *field) {
    this();
    concepts.addConceptIndex(this);
    for (A c : list(concepts, cc))
      updateImpl(c);
    register();
  }
  
  abstract void init();
  abstract void register();
  public abstract bool isApplicableValue(Val value);

  public void update(Concept c) {
    if (!isInstance(cc, c)) ret;
    updateImpl((A) c);
  }
  
  synchronized void updateImpl(A c) {
    Val newValue = cast cget(c, field);
    Val oldValue = objectToValue.get(c);
    if (newValue == oldValue && (oldValue != null || objectToValue.containsKey(c))) ret;
    valueToObject.remove(oldValue, c);
    if (isApplicableValue(newValue)) {
      valueToObject.put(newValue, c);
      put(objectToValue, c, newValue);
    } else
      main remove(objectToValue, c);
  }
  
  public synchronized void remove(Concept c) {
    if (!isInstance(cc, c)) ret;
    if (!objectToValue.containsKey(c)) ret;
    Val value = objectToValue.get(c);
    objectToValue.remove(c);
    valueToObject.remove(value, (A) c);
  }
  
  synchronized A get(Val value) {
    ret valueToObject.getFirst(value);
  }
  
  // older version - may return empty list as null
  // should be phased out everywhere!
  public synchronized Cl<A> getAll(Val value) {
    ret valueToObject.get(value);
  }
  
  // newer version - returns null iff it's not applicable
  public synchronized Cl<A> getAllIfApplicable(Val value) {
    if (!isApplicableValue(value)) null;
    ret unnullForIteration(valueToObject.get(value));
  }
  
  public synchronized L<Val> allValues() { ret cloneKeys_noSync(valueToObject.data); }
  
  IterableIterator<A> objectIterator() {
    ret navigableMultiSetMapValuesIterator_concurrent(valueToObject, this);
  }
  
  public synchronized MultiSet<Val> allValues_multiSet() {
    ret multiSetMapToMultiSet(valueToObject);
  }
  
  public Class<? extends Concept> conceptClass() { ret cc; }
  public int countConcepts() { ret l(objectToValue); }
  public Cl<Concept> allConcepts() { ret (Cl) keys(objectToValue); }
  
  O mutex() { this; }
}

Author comment

Began life as a copy of #1031103

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1032783
Snippet name: ConceptFieldIndexBase_certainValues
Eternal ID of this version: #1032783/4
Text MD5: ab7a32a14d2d37220310e81fa28f098a
Transpilation MD5: 5cd5786f5f75567b9d33761a0d146bdb
Author: stefan
Category: javax / concepts
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-10-06 04:25:18
Source code size: 2492 bytes / 78 lines
Pitched / IR pitched: No / No
Views / Downloads: 155 / 273
Version history: 3 change(s)
Referenced in: #1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674)