dbo CRUD first iter
This commit is contained in:
@@ -1,16 +1,23 @@
|
||||
package com.reliancy.rec;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.ListIterator;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
/** Base class of meta objects.
|
||||
* We use it to describe certain meta information. We derive from it Slot.
|
||||
* We define keys list of slots on the header level to describe slots.
|
||||
* We define keys list of slots on the header level to describe sub-slots.
|
||||
*
|
||||
* This class describes structure of Fields or Entities via the keys array of slots.
|
||||
* Additionally we provide a number of methods to locate, set or get or remove or add slots.
|
||||
* However slots could reside in other places such as base classes and so getOwnSlots will return a
|
||||
* bare list of slots in this object while all other methods will take into account other sources.
|
||||
* We do this to stay consistent at Rec level when Hdr inheritance comes into play.
|
||||
*/
|
||||
public class Hdr {
|
||||
public static final int FLAG_ARRAY =0x0001;
|
||||
public static final int FLAG_CHANGED =0x0002;
|
||||
public static final int FLAG_HIDDEN =0x0004;
|
||||
public static final int FLAG_STORABLE =0x0004;
|
||||
public static final int FLAG_LOCKED =0x0008;
|
||||
int flags;
|
||||
String name;
|
||||
@@ -30,8 +37,11 @@ public class Hdr {
|
||||
@Override
|
||||
public String toString(){
|
||||
StringBuilder ret=new StringBuilder();
|
||||
ret.append("{").append("flags:").append(flags).append(",name:").append(name);
|
||||
ret.append(",dim:").append(keys.size()).append("}");
|
||||
ret.append(name).append(":");
|
||||
ret.append("{")
|
||||
.append("flags:").append(flags)
|
||||
.append(",dim:").append(count())
|
||||
.append("}");
|
||||
return ret.toString();
|
||||
}
|
||||
|
||||
@@ -53,6 +63,9 @@ public class Hdr {
|
||||
public void setType(Class<?> type) {
|
||||
this.type = type;
|
||||
}
|
||||
public int getFlags(){
|
||||
return flags;
|
||||
}
|
||||
public Hdr raiseFlags(int f){
|
||||
flags|=f;
|
||||
return this;
|
||||
@@ -67,36 +80,51 @@ public class Hdr {
|
||||
public <T extends Hdr> T castAs(Class<T> clazz){
|
||||
return clazz.cast(this);
|
||||
}
|
||||
public int findSlot(String name){
|
||||
return findSlot(name,0);
|
||||
public List<Slot> getOwnSlots(){
|
||||
return keys;
|
||||
}
|
||||
public int findSlot(String name,int ofs){
|
||||
ListIterator<Slot> it=keys.listIterator(ofs);
|
||||
public boolean isOwned(Slot s){
|
||||
return keys.contains(s);
|
||||
}
|
||||
public Iterator<Slot> iterator(int offset){
|
||||
return keys.listIterator(offset);
|
||||
}
|
||||
public int indexOf(String name){
|
||||
return indexOf(name,0);
|
||||
}
|
||||
public int indexOf(String name,int ofs){
|
||||
Iterator<Slot> it=iterator(ofs);
|
||||
int index=-1;
|
||||
while(it.hasNext()){
|
||||
int index=it.nextIndex();
|
||||
index+=1;
|
||||
Slot e=it.next();
|
||||
if(e.getName().equalsIgnoreCase(name)) return index;
|
||||
//if(e.getName().equalsIgnoreCase(name)) return index;
|
||||
if(e.equals(name)) return index;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
public int findSlot(Slot s,int ofs){
|
||||
ListIterator<Slot> it=keys.listIterator(ofs);
|
||||
public int indexOf(Slot s,int ofs){
|
||||
Iterator<Slot> it=iterator(ofs);
|
||||
int index=-1;
|
||||
while(it.hasNext()){
|
||||
int index=it.nextIndex();
|
||||
index+=1;
|
||||
Slot e=it.next();
|
||||
if(e==s) return index;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
/**
|
||||
public Slot makeSlot(String name){
|
||||
return new Slot(name);
|
||||
}
|
||||
/**
|
||||
* this version will get or create a slot by given name.
|
||||
* @param name
|
||||
* @return
|
||||
*/
|
||||
public Slot getSlot(String name){
|
||||
int index=findSlot(name);
|
||||
public Slot getSlot(String name,boolean make){
|
||||
int index=indexOf(name);
|
||||
if(index<0){
|
||||
return new Slot(name);
|
||||
return make?makeSlot(name):null;
|
||||
}else{
|
||||
return getSlot(index);
|
||||
}
|
||||
@@ -116,11 +144,7 @@ public class Hdr {
|
||||
keys.set(index,s);
|
||||
return this;
|
||||
}
|
||||
public Slot[] slots(Slot... slots){
|
||||
if(slots!=null && slots.length>0){
|
||||
keys.clear();
|
||||
for(int i=0;i<slots.length;i++) keys.add(slots[i]);
|
||||
}
|
||||
return keys.toArray(new Slot[keys.size()]);
|
||||
public int count(){
|
||||
return keys.size();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,13 @@ public class JSON {
|
||||
public static final void writes(Rec rec,Appendable sink) throws IOException{
|
||||
JSONEncoder.encode(rec, sink);
|
||||
}
|
||||
|
||||
public static final String toString(Rec rec){
|
||||
StringBuffer buf=new StringBuffer();
|
||||
try {
|
||||
writes(rec,buf);
|
||||
} catch (IOException e) {
|
||||
}
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -189,7 +189,7 @@ public class JSONEncoder{
|
||||
if (o != null) {
|
||||
o.append(val.isArray()?"[":"{");
|
||||
}
|
||||
for (int i=0;i<val.count();i++) {
|
||||
for (int i=0;i<val.count();i++) {
|
||||
Slot k=val.getSlot(i);
|
||||
Object v=val.get(i);
|
||||
if (i > 0) {
|
||||
|
||||
@@ -118,7 +118,7 @@ public class Obj implements Rec{
|
||||
if(s==null) throw new IllegalArgumentException("invalid key provided");
|
||||
if(isArray()) throw new IllegalStateException("array not mappable with:"+s.getName());
|
||||
int index=s.getPosition(); // try slot position
|
||||
if(index<0) index=meta.findSlot(s.getName());// fall back to search if slot not set
|
||||
if(index<0) index=meta.indexOf(s.getName());// fall back to search if slot not set
|
||||
if(index<0){
|
||||
values.add(val);
|
||||
meta.addSlot(s);
|
||||
@@ -138,14 +138,14 @@ public class Obj implements Rec{
|
||||
if(s==null) throw new IllegalArgumentException("invalid key provided");
|
||||
//if(keys==null) throw new IllegalStateException("array not mappable with:"+s.getName());
|
||||
int index=s.getPosition(); // try slot position
|
||||
if(index<0 && !isArray()) index=meta.findSlot(s.getName());// fall back to search if slot not set
|
||||
if(index<0 && !isArray()) index=meta.indexOf(s.getName());// fall back to search if slot not set
|
||||
return index<0?def:values.get(index);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Rec remove(Slot s) {
|
||||
int index=s.getPosition(); // try slot position
|
||||
if(index<0 && !isArray()) index=meta.findSlot(s.getName());// fall back to search if slot not set
|
||||
if(index<0 && !isArray()) index=meta.indexOf(s.getName());// fall back to search if slot not set
|
||||
if(index>=0) remove(index);
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ public interface Rec extends Vec{
|
||||
public Rec remove(Slot s);
|
||||
public default Slot getSlot(String name){
|
||||
Hdr m=meta();
|
||||
return m!=null?m.getSlot(name):null;
|
||||
return m!=null?m.getSlot(name,true):null;
|
||||
}
|
||||
public default Slot getSlot(int pos){
|
||||
Hdr m=meta();
|
||||
|
||||
@@ -11,7 +11,7 @@ public class Slot extends Hdr {
|
||||
Object getInitalValue(Slot s,Rec rec);
|
||||
}
|
||||
public static final Initializer DEFAULT_INITIALIZER=new Initializer(){
|
||||
public Object getInitalValue(Slot s,Rec rec) {return s.getDefaultValue();}
|
||||
public Object getInitalValue(Slot s,Rec rec) {return s.getInitValue();}
|
||||
};
|
||||
int position;
|
||||
Object defaultValue;
|
||||
@@ -25,23 +25,29 @@ public class Slot extends Hdr {
|
||||
this.position=-1;
|
||||
this.initValue=DEFAULT_INITIALIZER;
|
||||
}
|
||||
public boolean equals(String str){
|
||||
return name.equalsIgnoreCase(str);
|
||||
}
|
||||
public int getPosition() {
|
||||
return position;
|
||||
}
|
||||
public void setPosition(int position) {
|
||||
public Slot setPosition(int position) {
|
||||
this.position = position;
|
||||
return this;
|
||||
}
|
||||
public Object getDefaultValue() {
|
||||
public Object getInitValue() {
|
||||
return defaultValue;
|
||||
}
|
||||
public void setDefaultValue(Object defaultValue) {
|
||||
public Slot setInitValue(Object defaultValue) {
|
||||
this.defaultValue = defaultValue;
|
||||
return this;
|
||||
}
|
||||
public Initializer getInitValue() {
|
||||
public Initializer getInitVia() {
|
||||
return initValue;
|
||||
}
|
||||
public void setInitValue(Initializer initValue) {
|
||||
public Slot setInitVia(Initializer initValue) {
|
||||
this.initValue = initValue;
|
||||
return this;
|
||||
}
|
||||
public int toString(Object val, StringBuilder buf) {
|
||||
int length0=buf.length();
|
||||
|
||||
Reference in New Issue
Block a user