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

66
LINES

< > BotCompany Repo | #1031103 - ConceptFieldIndexBase

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

Libraryless. Click here for Pure Java version (15411L/99K).

asclass ConceptFieldIndexBase<A extends Concept, Val> implements IConceptIndex, IFieldIndex<A, Val>, IConceptCounter {
  Concepts concepts;
  Class<A> cc;
  S field;
  new HashMap<A, Val> objectToValue;
  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 void update(Concept c) {
    if (!isInstance(cc, c)) ret;
    updateImpl(c);
  }
  
  synchronized void updateImpl(Concept c) {
    Val newValue = cast cget(c, field);
    Val oldValue = objectToValue.get(c);
    if (newValue == null || newValue != oldValue) {
      valueToObject.remove(oldValue, (A) c);
      valueToObject.put(newValue, (A) c);
      syncPut(objectToValue, (A) c, newValue);
    }
  }
  
  public synchronized void remove(Concept c) {
    if (!isInstance(cc, c)) ret;
    Val value = cast cget(c, field);
    syncRemove(objectToValue, (A) c);
    valueToObject.remove(value, (A) c);
  }
  
  synchronized A get(Val value) {
    ret valueToObject.getFirst(value);
  }
  
  public synchronized Cl<A> getAll(Val value) {
    ret 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 #1014416

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx

No comments. add comment

Snippet ID: #1031103
Snippet name: ConceptFieldIndexBase
Eternal ID of this version: #1031103/5
Text MD5: 67a2f0c6b2c05d4d4be4a0a5724043f1
Transpilation MD5: 55f7b664aa152cdbb8dd4bf9dbb7400c
Author: stefan
Category: javax / concepts
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-05-03 22:02:40
Source code size: 1976 bytes / 66 lines
Pitched / IR pitched: No / No
Views / Downloads: 11 / 33
Version history: 4 change(s)
Referenced in: [show references]

Formerly at http://tinybrain.de/1031103 & http://1031103.tinybrain.de