001/*
002 *                    BioJava development code
003 *
004 * This code may be freely distributed and modified under the
005 * terms of the GNU Lesser General Public Licence.  This should
006 * be distributed with the code.  If you do not have a copy,
007 * see:
008 *
009 *      http://www.gnu.org/copyleft/lesser.html
010 *
011 * Copyright for this code is held jointly by the individual
012 * authors.  These should be listed in @author doc comments.
013 *
014 * For more information on the BioJava project and its aims,
015 * or to join the biojava-l mailing list, visit the home page
016 * at:
017 *
018 *      http://www.biojava.org/
019 *
020 */
021package org.biojava.nbio.aaproperties.xml;
022
023import javax.xml.bind.annotation.XmlAccessType;
024import javax.xml.bind.annotation.XmlAccessorType;
025import javax.xml.bind.annotation.XmlAttribute;
026import javax.xml.bind.annotation.XmlElement;
027import java.util.HashMap;
028import java.util.List;
029import java.util.Map;
030
031/**
032 * One way to model the elements
033 * @author pvtroshin
034 *
035 */
036@XmlAccessorType(XmlAccessType.NONE)
037public class Element {
038
039        /**
040         * Element name as per periodic table e.g. "Hydrogen"
041         */
042        @XmlAttribute(name = "name", required = true)
043        private String name;
044        /**
045         * Element short name as in periodic table e.g. "H"
046         */
047        @XmlAttribute(name = "symbol")
048        private String symbol;
049        /**
050         * The atomic number of the element = number of protons.
051         */
052        @XmlAttribute(name = "atomicnumber")
053        private int atomicNumber;
054        /**
055         * The computed mass based on isotopes and their abundances
056         */
057        @XmlAttribute(name = "mass", required = true)
058        private double mass;
059        /**
060         * List of common isotopes of the element
061         */
062        @XmlElement
063        private List<Isotope> isotope;
064
065        /**
066         * To enable quick retrieval of Isotope from its name
067         */
068        private Map<String, Isotope> name2Isotope;
069
070        public Element(){}
071
072        public Element(String name, String symbol, int atomicNumber, List<Isotope> isotopes, double mass){
073                if(atomicNumber <= 0){
074                        throw new Error("Atomic number of Elements must be > 0.");
075                }
076                if(mass <= 0){
077                        throw new Error("Mass of Elements must be > 0.");
078                }
079                this.setName(name);
080                this.setSymbol(symbol);
081                this.setAtomicNumber(atomicNumber);
082                this.setIsotopes(isotopes);
083                this.setMass(mass);
084        }
085
086        @Override
087        public String toString(){
088                return symbol + ", " + name + ", " + atomicNumber;
089        }
090
091        public void setMass(double mass){
092                this.mass = mass;
093        }
094
095        public double getMass(){
096                return this.mass;
097        }
098
099        public void setName(String name) {
100                this.name = name;
101        }
102
103        public String getName(){
104                return this.name;
105        }
106
107        public void setSymbol(String symbol) {
108                this.symbol = symbol;
109        }
110
111        public void setAtomicNumber(int atomicNumber) {
112                this.atomicNumber = atomicNumber;
113        }
114
115
116        public List<Isotope> getIsotopes() {
117                return isotope;
118        }
119
120
121        public void setIsotopes(List<Isotope> isotopes) {
122                this.isotope = isotopes;
123                this.name2Isotope = new HashMap<String, Isotope>();
124                if(isotopes != null){
125                        for(Isotope i:isotopes){
126                                name2Isotope.put(i.getName(), i);
127                        }
128                }
129        }
130}