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

64
LINES

< > BotCompany Repo | #1034593 // Entity etc.

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

Libraryless. Click here for Pure Java version (20319L/123K).

concept Entity > NamedConcept {
}

abstract concept AbstractJavaObjectEntity<A> > Entity {
  abstract A object();
  
  toString {
    S s = super.toString();
    O o = object();
    if (o != null)
      s += " " + str_pcall(o);
    ret s;
  }
}

concept TransientJavaObjectEntity<A> > AbstractJavaObjectEntity<A> {
  gettable transient A object;
}

concept PersistentJavaObjectEntity<A> > AbstractJavaObjectEntity<A> {
  gettable A object;
}

concept ConceptEntity<A extends Concept> extends AbstractJavaObjectEntity<A> {
  new Ref<A> concept;
  
  *(A concept) { this.concept.set(concept); }
  
  A object() { ret concept!; }
}

concept FieldInObjectEntity > Entity {
  new Ref<AbstractJavaObjectEntity<?>> objectEntity;
  S fieldName;
  
  *(AbstractJavaObjectEntity objectEntity, S *fieldName) {
    this.objectEntity.set(objectEntity);
  }
  
  O value() { ret !objectEntity.has() ? null : getOpt(objectEntity->object(), fieldName); }
  
  toString {
    S s = super.toString() + " " + fieldName + " in " + objectEntity;
    O value = value();
    if (value != null)
      s += appendBracketed("value of type " + className(value));
    ret s;
  }
}

concept DeclaredFieldEntity > Entity {
  Class theClass;
  S fieldName;
  
  *(Class *theClass, S *fieldName) {}
  
  Field fieldObject() { ret findFieldOfClass(theClass, fieldName); }
  
  toString {
    var fieldObject = fieldObject();
    ret super.toString() + " " + (fieldObject != null ? str(fieldObject)
      : "[field not found: " + className(theClass) + "." + fieldName + "]");
  }
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1034593
Snippet name: Entity etc.
Eternal ID of this version: #1034593/16
Text MD5: 6e891775dd5485e0e9efec1f5a4a16db
Transpilation MD5: c0342ea189530a9f8affc30846ab082e
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-02-19 09:52:04
Source code size: 1610 bytes / 64 lines
Pitched / IR pitched: No / No
Views / Downloads: 169 / 783
Version history: 15 change(s)
Referenced in: [show references]