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

de.tsl2.nano.bean.BReference Maven / Gradle / Ivy

Go to download

TSL2 Framework Descriptor (currency-handling, generic formatter, descriptors for beans, collections, actions and values)

There is a newer version: 2.5.1
Show newest version
/*
 * File: $HeadURL$
 * Id  : $Id$
 * 
 * created by: Tom
 * created on: 02.03.2016
 * 
 * Copyright: (c) Thomas Schneider 2016, all rights reserved
 */
package de.tsl2.nano.bean;

import de.tsl2.nano.bean.def.Bean;
import de.tsl2.nano.core.cls.AReference;
import de.tsl2.nano.core.cls.BeanAttribute;
import de.tsl2.nano.core.cls.BeanClass;
import de.tsl2.nano.core.util.StringUtil;

/**
 * implements a detach/attach or de-/materialize or pointer/content algorithm for entities using the BeanContainer.
 * 
 * @author Tom
 * @version $Revision$
 */
public class BReference extends AReference, O> {

    /**
     * constructor for deserialization
     */
    protected BReference() {
    }
    
    public BReference(O instance) {
        super(instance);
    }

    @SuppressWarnings("unchecked")
    @Override
    protected Class type(String description) {
        return BeanClass.load(StringUtil.substring(description, PREFIX_REFERENCE, PREFIX_ID));
    }
    
    @Override
    protected Object id(Class type, String strId) {
        return BeanAttribute.wrap(strId, BeanContainer.getIdAttribute(type).getType());
    }
    
    public String name() {
        return StringUtil.substring(super.toString(), null, ":");
    }
    
    @Override
    protected Object getId(Object instance) {
        return BeanContainer.getIdAttribute(resolve()).getValue(instance);
    }

    @Override
    protected O materialize(String description) {
        if (!BeanContainer.isConnected())
            return null;
        Pointer tid = getTypeAndId(description);
        return BeanContainer.instance().getByID(tid.type, tid.id);
    }

    public Bean bean() {
        return resolve() != null ? Bean.getBean(instance) : null;
    }
    
    @Override
    public String toString() {
        return instance != null ? Bean.getBean(instance).toString() : super.toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy