<commit_before>package org.nakedobjects.reflector.java;

import org.nakedobjects.NakedObjects;
import org.nakedobjects.application.BusinessObjectContainer;
import org.nakedobjects.object.NakedObject;
import org.nakedobjects.object.NakedObjectSpecification;
import org.nakedobjects.object.TypedNakedCollection;
import org.nakedobjects.object.persistence.NakedObjectManager;

import java.util.Enumeration;
import java.util.Vector;

import org.apache.log4j.Logger;


public class JavaBusinessObjectContainer implements BusinessObjectContainer {
    private static final Logger LOG = Logger.getLogger(JavaBusinessObjectContainer.class);

    private NakedObject adapterFor(Object object) {
        NakedObject adapter = NakedObjects.getObjectLoader().getAdapterForElseCreateAdapterForTransient(object);

        return adapter;
    }

    public Vector allInstances(Class cls) {
        return allInstances(cls, false);
    }

    public Vector allInstances(Class cls, boolean includeSubclasses) {
        TypedNakedCollection nakedObjectInstances = objectManager().allInstances(getSpecification(cls), includeSubclasses);
        Vector objectInstances = new Vector(nakedObjectInstances.size());
        Enumeration e = nakedObjectInstances.elements();
        while (e.hasMoreElements()) {
            NakedObject instance = (NakedObject) e.nextElement();
            objectInstances.addElement(instance.getObject());
        }
        return objectInstances;
    }

    /**
     * Creates a new instance and then persists it.
     * 
     * @see JavaBusinessObjectContainer#createTransientInstance(Class) for details of object creation
     */
    public Object createInstance(Class cls) {
        LOG.debug("creating new persistent instance of " + cls.getName());
        NakedObject object = objectManager().createPersistentInstance(cls.getName());
        return object.getObject();
    }

    /**
     * Creates a new instance of the specified type, and then call the new objects setContainer() and created() methods if they
     * exist.
     */
    public Object createTransientInstance(Class cls) {
        LOG.debug("creating new tranisent instance of " + cls.getName());
        NakedObject object = objectManager().createTransientInstance(cls.getName());
        return object.getObject();
    }

    public void destroyObject(Object object) {
        //objectManager().destroyObject(NakedObjects.getPojoAdapterFactory().createNOAdapter(object));
        objectManager().destroyObject(adapterFor(object));
    }

    protected void finalize() throws Throwable {
        super.finalize();
        LOG.info("finalizing java business object container " + this);
    }

    private NakedObjectSpecification getSpecification(Class cls) {
        return NakedObjects.getSpecificationLoader().loadSpecification(cls);
    }

    public boolean hasInstances(Class cls) {
        return objectManager().hasInstances(getSpecification(cls));
    }

    public void makePersistent(Object transientObject) {
        NakedObject adapter = adapterFor(transientObject);
        objectManager().startTransaction();
        objectManager().makePersistent(adapter);
        objectManager().endTransaction();
    }

    public int numberOfInstances(Class cls) {
        return objectManager().numberOfInstances(getSpecification(cls));
    }

    public void objectChanged(Object object) {
        if (object != null) {
            NakedObject adapter = adapterFor(object);
            objectManager().objectChanged(adapter);
        }
    }

    private NakedObjectManager objectManager() {
        return NakedObjects.getObjectManager();
    }

    public void resolve(Object parent, Object field) {
        if (field == null) {
            NakedObject adapter = adapterFor(parent);
            if (adapter.isPersistent() && !adapter.isResolved()) {
                objectManager().resolveImmediately(adapter);
            }
        }

        /*
         * 
         * if (field != null) { NakedObject adapter = adapterFor(field); if (adapter.isPersistent() && !adapter.isResolved()) {
         * objectManager().resolveImmediately(adapter); } }
         */
    }

    /**
     * Generates a unique serial number for the specified squence set. Each set of serial numbers are a simple numerical sequence.
     * Calling this method with a unused sequence name creates a new set.
     */
    public long serialNumber(String sequence) {
        LOG.debug("serialNumber " + sequence);

        Vector instances = allInstances(Sequence.class, false);
        Sequence number;

        for (Enumeration e = instances.elements(); e.hasMoreElements();) {
            number = (Sequence) e.nextElement();
            if (number.getName().isSameAs(sequence)) {
                number.getSerialNumber().next();
                objectChanged(number);
                return number.getSerialNumber().longValue();
            }
        }

        number = (Sequence) createTransientInstance(Sequence.class);
        number.getName().setValue(sequence);
        makePersistent(number);
        return number.getSerialNumber().longValue();
    }

    public void userMessage(String text) {}
}

/*<commit_msg>Refactoring to tidy up resolving and combine object factory and manager - V<commit_after>package org.nakedobjects.reflector.java;

import org.nakedobjects.NakedObjects;
import org.nakedobjects.application.BusinessObjectContainer;
import org.nakedobjects.object.NakedObject;
import org.nakedobjects.object.NakedObjectSpecification;
import org.nakedobjects.object.ResolveState;
import org.nakedobjects.object.TypedNakedCollection;
import org.nakedobjects.object.persistence.NakedObjectManager;

import java.util.Enumeration;
import java.util.Vector;

import org.apache.log4j.Logger;


public class JavaBusinessObjectContainer implements BusinessObjectContainer {
    private static final Logger LOG = Logger.getLogger(JavaBusinessObjectContainer.class);

    private NakedObject adapterFor(Object object) {
        NakedObject adapter = NakedObjects.getObjectLoader().getAdapterForElseCreateAdapterForTransient(object);

        return adapter;
    }

    public Vector allInstances(Class cls) {
        return allInstances(cls, false);
    }

    public Vector allInstances(Class cls, boolean includeSubclasses) {
        TypedNakedCollection nakedObjectInstances = objectManager().allInstances(getSpecification(cls), includeSubclasses);
        Vector objectInstances = new Vector(nakedObjectInstances.size());
        Enumeration e = nakedObjectInstances.elements();
        while (e.hasMoreElements()) {
            NakedObject instance = (NakedObject) e.nextElement();
            objectInstances.addElement(instance.getObject());
        }
        return objectInstances;
    }

    /**
     * Creates a new instance and then persists it.
     * 
     * @see JavaBusinessObjectContainer#createTransientInstance(Class) for details of object creation
     */
    public Object createInstance(Class cls) {
        LOG.debug("creating new persistent instance of " + cls.getName());
        NakedObject object = objectManager().createPersistentInstance(cls.getName());
        return object.getObject();
    }

    /**
     * Creates a new instance of the specified type, and then call the new objects setContainer() and created() methods if they
     * exist.
     */
    public Object createTransientInstance(Class cls) {
        LOG.debug("creating new tranisent instance of " + cls.getName());
        NakedObject object = objectManager().createTransientInstance(cls.getName());
        return object.getObject();
    }

    public void destroyObject(Object object) {
        //objectManager().destroyObject(NakedObjects.getPojoAdapterFactory().createNOAdapter(object));
        objectManager().destroyObject(adapterFor(object));
    }

    protected void finalize() throws Throwable {
        super.finalize();
        LOG.info("finalizing java business object container " + this);
    }

    private NakedObjectSpecification getSpecification(Class cls) {
        return NakedObjects.getSpecificationLoader().loadSpecification(cls);
    }

    public boolean hasInstances(Class cls) {
        return objectManager().hasInstances(getSpecification(cls));
    }

    public void makePersistent(Object transientObject) {
        NakedObject adapter = adapterFor(transientObject);
        objectManager().startTransaction();
        objectManager().makePersistent(adapter);
        objectManager().endTransaction();
    }

    public int numberOfInstances(Class cls) {
        return objectManager().numberOfInstances(getSpecification(cls));
    }

    public void objectChanged(Object object) {
        if (object != null) {
            NakedObject adapter = adapterFor(object);
            objectManager().objectChanged(adapter);
        }
    }

    private NakedObjectManager objectManager() {
        return NakedObjects.getObjectManager();
    }

    public void resolve(Object parent, Object field) {
        if (field == null) {
            NakedObject adapter = adapterFor(parent);
            ResolveState resolveState = adapter.getResolveState();
            if (resolveState.isResolvable(ResolveState.RESOLVING)) {
                objectManager().resolveImmediately(adapter);
            }
        }
    }

    /**
     * Generates a unique serial number for the specified squence set. Each set of serial numbers are a simple numerical sequence.
     * Calling this method with a unused sequence name creates a new set.
     */
    public long serialNumber(String sequence) {
        LOG.debug("serialNumber " + sequence);

        Vector instances = allInstances(Sequence.class, false);
        Sequence number;

        for (Enumeration e = instances.elements(); e.hasMoreElements();) {
            number = (Sequence) e.nextElement();
            if (number.getName().isSameAs(sequence)) {
                number.getSerialNumber().next();
                objectChanged(number);
                return number.getSerialNumber().longValue();
            }
        }

        number = (Sequence) createTransientInstance(Sequence.class);
        number.getName().setValue(sequence);
        makePersistent(number);
        return number.getSerialNumber().longValue();
    }

    public void userMessage(String text) {}
}

/*
{'fraction_non_alphanumeric': 0.09511343804537521, 'fraction_numerical': 0.00019391118867558658, 'mean_word_length': 3.1542488924687877, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1468717', 'n_tokens_mistral': 2613, 'n_tokens_neox': 2560, 'n_words': 597}