<commit_before>package etomica.atom.iterator;

import java.io.Serializable;

import etomica.atom.AtomAddressManager;
import etomica.atom.IAtom;
import etomica.box.Box;
import etomica.species.Species;

/**
 * Gives pairs formed from the molecules of a species in a box, taking one
 * molecule the species with all of its other molecules. Species is specified at
 * construction and cannot be changed afterwards.
 */

public class ApiIntraspecies1A extends ApiSequence1A implements
        AtomsetIteratorPDT, Serializable {

    /**
     * @param species
     *            species whose molecules will form the pair iterates
     */
    public ApiIntraspecies1A(Species species) {
        this(new Species[] { species, species });
    }

    /**
     * @param species
     *            array of two non-null elements referencing the same species
     *            instance
     * @throws NullPointerException
     *             if species or one of its elements is null
     * @throws IllegalArgumentException
     *             if species is not a length-2 array or if its elements refer
     *             to different species instances
     */
    public ApiIntraspecies1A(Species[] species) {
        super();
        if (species.length != 2)
            throw new IllegalArgumentException(
                    "Constructor of ApiIntraspecies1A requires two references to the same species instance");
        if (species == null || species[0] == null || species[1] == null)
            throw new NullPointerException(
                    "Constructor of ApiIntraspecies1A requires two non-null species references to the same instance");
        if (species[0] != species[1])
            throw new IllegalArgumentException(
                    "Constructor of ApiIntraspecies1A requires references to the same species instance");
        this.species = species[0];
    }

    /**
     * Configures iterator to return molecules from the set species in the given
     * box.
     * @throws NullPointerException if the Box is null
     */
    public void setBox(Box box) {
        if (box == null) {
            throw new IllegalArgumentException("You are a bad person.  I didn't even care about the box, but since you passed null, I'm going to quit.");
        }
        identifyTargetMolecule();
    }

    /**
     * Sets the target molecule with which all pairs are formed. Molecule is
     * determined from the atom specified by the atomSet (which must have
     * count() == 1), which may be the molecule itself or an atom that is part
     * of it. If the atom is not in the species given at
     * construction, no iterates will be returned.
     * 
     * @throws NullPointerException
     *             if targetAtom is null
     */
    public void setTarget(IAtom newTargetAtom) {
        if (newTargetAtom == null) {
            throw new NullPointerException("target atom must not be null");
        }
        targetAtom = newTargetAtom;
        identifyTargetMolecule();
    }

    /**
     * Finds target molecule as indicated by the target atom. Sets target
     * molecule to null if target atom is null, box is null, or atom is not
     * part of either species.
     */
    private void identifyTargetMolecule() {
        if (targetAtom == null) {
            targetMolecule = null;
        } else {
            targetMolecule = targetAtom;
            while (targetMolecule.getType().getDepth() > AtomAddressManager.MOLECULE_DEPTH) {
                targetMolecule = targetMolecule.getParentGroup();
            }
            if (targetMolecule.getType().getSpecies() != species) {
                targetMolecule = null;
            }
        }
        //targetMolecule may be null here
        setAtom(targetMolecule);
    }

    private static final long serialVersionUID = 2L;
    private final Species species;

    private IAtom targetAtom, targetMolecule;

}<commit_msg>only attempt to identify the target molecule from reset<commit_after>package etomica.atom.iterator;

import java.io.Serializable;

import etomica.atom.AtomAddressManager;
import etomica.atom.IAtom;
import etomica.box.Box;
import etomica.species.Species;

/**
 * Gives pairs formed from the molecules of a species in a box, taking one
 * molecule the species with all of its other molecules. Species is specified at
 * construction and cannot be changed afterwards.
 */

public class ApiIntraspecies1A extends ApiSequence1A implements
        AtomsetIteratorPDT, Serializable {

    /**
     * @param species
     *            species whose molecules will form the pair iterates
     */
    public ApiIntraspecies1A(Species species) {
        this(new Species[] { species, species });
    }

    /**
     * @param species
     *            array of two non-null elements referencing the same species
     *            instance
     * @throws NullPointerException
     *             if species or one of its elements is null
     * @throws IllegalArgumentException
     *             if species is not a length-2 array or if its elements refer
     *             to different species instances
     */
    public ApiIntraspecies1A(Species[] species) {
        super();
        if (species.length != 2)
            throw new IllegalArgumentException(
                    "Constructor of ApiIntraspecies1A requires two references to the same species instance");
        if (species == null || species[0] == null || species[1] == null)
            throw new NullPointerException(
                    "Constructor of ApiIntraspecies1A requires two non-null species references to the same instance");
        if (species[0] != species[1])
            throw new IllegalArgumentException(
                    "Constructor of ApiIntraspecies1A requires references to the same species instance");
        this.species = species[0];
    }

    /**
     * Configures iterator to return molecules from the set species in the given
     * box.
     * @throws NullPointerException if the Box is null
     */
    public void setBox(Box box) {
        if (box == null) {
            throw new IllegalArgumentException("You are a bad person.  I didn't even care about the box, but since you passed null, I'm going to quit.");
        }
    }

    /**
     * Sets the target molecule with which all pairs are formed. Molecule is
     * determined from the atom specified by the atomSet (which must have
     * count() == 1), which may be the molecule itself or an atom that is part
     * of it. If the atom is not in the species given at
     * construction, no iterates will be returned.
     * 
     * @throws NullPointerException
     *             if targetAtom is null
     */
    public void setTarget(IAtom newTargetAtom) {
        if (newTargetAtom == null) {
            throw new NullPointerException("target atom must not be null");
        }
        targetAtom = newTargetAtom;
    }
    
    public void reset() {
        identifyTargetMolecule();
        super.reset();
    }

    /**
     * Finds target molecule as indicated by the target atom. Sets target
     * molecule to null if target atom is null, box is null, or atom is not
     * part of either species.
     */
    private void identifyTargetMolecule() {
        if (targetAtom == null) {
            targetMolecule = null;
        } else {
            targetMolecule = targetAtom;
            while (targetMolecule.getType().getDepth() > AtomAddressManager.MOLECULE_DEPTH) {
                targetMolecule = targetMolecule.getParentGroup();
            }
            if (targetMolecule.getType().getSpecies() != species) {
                targetMolecule = null;
            }
        }
        //targetMolecule may be null here
        setAtom(targetMolecule);
    }

    private static final long serialVersionUID = 2L;
    private final Species species;

    private IAtom targetAtom, targetMolecule;

}
{'fraction_non_alphanumeric': 0.07188295165394402, 'fraction_numerical': 0.004071246819338422, 'mean_word_length': 2.2483471074380166, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '7310638', 'n_tokens_mistral': 2068, 'n_tokens_neox': 1985, 'n_words': 759}