001    /*
002     * mesopotamia @mesopotamia.version@
003     * Multilingual parser and repository. 
004     * Copyright (C) 2005  Hammurapi Group
005     *
006     * This program is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU Lesser General Public
008     * License as published by the Free Software Foundation; either
009     * version 2 of the License, or (at your option) any later version.
010     *
011     * This program is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014     * Lesser General Public License for more details.
015     *
016     * You should have received a copy of the GNU Lesser General Public
017     * License along with this library; if not, write to the Free Software
018     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
019     *
020     * URL: http://http://www.hammurapi.biz
021     * e-Mail: support@hammurapi.biz
022     */
023    package org.mesopotamia;
024    
025    import java.io.Serializable;
026    
027    import org.w3c.dom.Element;
028    
029    import biz.hammurapi.xml.dom.DomSerializable;
030    
031    public class LanguageElementHandle implements Serializable, DomSerializable {
032            private Class contextClass;
033            private Class targetClass;
034            private int id;
035            private int sourceUnitId;
036                    
037            private String targetClassName;
038            private String contextClassName;        
039    
040            /**
041             * @param id
042             * @param contextClass
043             */
044            public LanguageElementHandle(int sourceUnitId, int id, Class contextClass, Class targetClass) {
045                    super();
046                    this.sourceUnitId = sourceUnitId;
047                    this.id = id;
048                    this.contextClass = contextClass;
049                    if (contextClass!=null) {
050                            contextClassName=contextClass.getName();
051                    }
052                    this.targetClass = targetClass;
053                    if (targetClass!=null) {
054                            targetClassName=targetClass.getName();
055                    }
056            }
057    
058            public Class getContextClass() {
059                    return contextClass;
060            }
061    
062            public Class getTargetClass() {
063                    return targetClass;
064            }
065    
066            public int getId() {
067                    return id;
068            }
069            
070            public int getSourceUnitId() {
071                    return sourceUnitId;
072            }
073    
074            public void toDom(Element holder) {
075                    holder.setAttribute("id", String.valueOf(id));
076                    holder.setAttribute("source-unit", String.valueOf(sourceUnitId));
077                    if (contextClass!=null) {
078                            holder.setAttribute("context", contextClass.getName());
079                    }
080                    if (targetClass!=null) {
081                            holder.setAttribute("target", targetClass.getName());
082                    }
083            }       
084            
085            public int hashCode() {
086                    final int PRIME = 31;
087                    int result = 1;
088                    result = PRIME * result + ((contextClassName == null) ? 0 : contextClassName.hashCode());
089                    result = PRIME * result + id;
090                    result = PRIME * result + sourceUnitId;
091                    result = PRIME * result + ((targetClassName == null) ? 0 : targetClassName.hashCode());
092                    return result;
093            }
094    
095            public boolean equals(Object obj) {
096                    if (this == obj)
097                            return true;
098                    if (obj == null)
099                            return false;
100                    if (getClass() != obj.getClass())
101                            return false;
102                    final LanguageElementHandle other = (LanguageElementHandle) obj;
103                    if (id != other.id) {
104                            return false;
105                    }
106                    if (sourceUnitId != other.sourceUnitId) {
107                            return false;
108                    }
109                    
110                    if (contextClassName == null) {
111                            if (other.contextClassName != null) {
112                                    return false;
113                            }
114                    } else if (!contextClassName.equals(other.contextClassName)) {
115                            return false;
116                    }
117                    
118                    if (targetClassName == null) {
119                            if (other.targetClassName != null) {
120                                    return false;
121                            }
122                    } else if (!targetClassName.equals(other.targetClassName)) {
123                            return false;
124                    }
125                    
126                    return true;
127            }
128    
129            public String toString() {
130                    return "[Language element handle] "+sourceUnitId+"/"+id+" ("+contextClass+") -> "+targetClass;
131            }
132    }