All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.nutz.dao.impl.entity.EntityObjectContext Maven / Gradle / Ivy

Go to download

Nutz, which is a collections of lightweight frameworks, each of them can be used independently

There is a newer version: 1.r.72
Show newest version
package org.nutz.dao.impl.entity;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.nutz.dao.entity.Entity;
import org.nutz.dao.entity.MappingField;
import org.nutz.lang.util.AbstractContext;
import org.nutz.lang.util.Context;

public class EntityObjectContext extends AbstractContext {

    private static final String ME = "$me";

    private Map ext = new HashMap();

    private Entity en;
    private Object obj;

    public EntityObjectContext(Entity en, Object obj) {
        this.en = en;
        this.obj = obj;
    }

    public int size() {
        return ext.size();
    }

    public Context set(String name, Object value) {
        MappingField field = en.getField(name);
        if (field != null && !("view".equals(name) || "field".equals(name)))
            field.setValue(obj, value);
        else
            ext.put(name, value);
        return this;
    }

    public Set keys() {
        Set names = new HashSet(en.getMappingFields().size());
        names.add(ME);
        for (MappingField mf : en.getMappingFields())
            names.add(mf.getName());
        names.addAll(ext.keySet());
        return names;
    }

    public boolean has(String key) {
        if (ME.equals(key))
            return true;
        if (en.getField(key) != null)
            return true;
        return ext.containsKey(key);
    }

    public Context clear() {
        obj = en.getMirror().born();
        ext.clear();
        return this;
    }

    public Object get(String name) {
        if (ME.equals(name))
            return obj;
        MappingField field = en.getField(name);
        if (field != null)
            return field.getValue(obj);
        return ext.get(name);
    }

    public EntityObjectContext clone() {
        EntityObjectContext eoc = new EntityObjectContext(en, obj);
        if (!this.ext.isEmpty())
            eoc.ext = new HashMap(this.ext);
        return eoc;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy