001    /*
002     * Copyright (c) 1998-2014 ChemAxon Ltd. All Rights Reserved.
003     *
004     * This software is the confidential and proprietary information of
005     * ChemAxon. You shall not disclose such Confidential Information
006     * and shall use it only in accordance with the terms of the agreements
007     * you entered into with ChemAxon.
008     *
009     */
010    package com.chemaxon.descriptors.alignment;
011    
012    import com.chemaxon.common.annotations.PublicAPI;
013    import com.chemaxon.descriptors.common.DescriptorGenerator;
014    import com.google.common.annotations.Beta;
015    import org.apache.commons.codec.binary.StringUtils;
016    /**
017     *
018     *
019     * @author Adrian Kalaszi
020     * @param <D>
021     * @param <P>
022     */
023    @Beta
024    @PublicAPI
025    public abstract class AbstractShapeGenerator<D extends ShapeDescriptor, P extends AbstractShapeGeneratorParameter> implements DescriptorGenerator<D> {
026    
027        protected final P parameters;
028    
029        public AbstractShapeGenerator(final P parameters) {
030            this.parameters = parameters;
031        }
032    
033        @Override
034        public byte[] toByteArray(final D desc) {
035            return desc.toByteArray();
036        }
037    
038        @Override
039        public String toString(final D desc) {
040            return StringUtils.newStringUtf8(toByteArray(desc));
041        }
042    
043        @Override
044        public D fromString(final String desc) {
045            return fromByteArray(StringUtils.getBytesUtf8(desc));
046        }
047    
048        @Override
049        public P getParameters() {
050            return parameters;
051        }
052    
053        @Override
054        public String toString() {
055            final StringBuilder builder = new StringBuilder(80);
056            builder.append("Shape properties\nAtom typing for shape:");
057            builder.append(parameters.getAtomTyping());
058            return builder.toString();
059        }
060    
061        @Override
062        public int hashCode() {
063            final int prime = 31;
064            int result = 1;
065            result = prime * result + ((parameters == null) ? 0 : parameters.hashCode());
066            return result;
067        }
068    
069        @Override
070        public boolean equals(final Object obj) {
071            if (this == obj) {
072                return true;
073            }
074            if (!(obj instanceof AbstractShapeGenerator)) {
075                return false;
076            }
077            final AbstractShapeGenerator<D,P> other = (AbstractShapeGenerator) obj;
078            return this.parameters.equals(other.parameters);
079        }
080    
081        /*
082         * (non-Javadoc)
083         * @see
084         * com.chemaxon.descriptors.common.DescriptorGenerator#getBareDescriptor(com.chemaxon.descriptors.common.Descriptor)
085         */
086        @Override
087        public D getBareDescriptor(D descriptor) {
088            return descriptor;
089        }
090    
091        @Override
092        public Object getGuardObject() {
093            return this;
094        }
095    
096    }