Package net.laubenberger.tyr.model

Source Code of net.laubenberger.tyr.model.ModuleDataImpl$XmlAdapterModuleData

/*
* Copyright (c) 2010-2012 by Stefan Laubenberger.
*
* Tyr is free software: you can redistribute it and/or modify
* it under the terms of the General Public License v2.0.
*
* Tyr is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* See the GNU General Public License for more details:
* <http://www.gnu.org/licenses>
*
* This distribution is available at:
* <http://code.google.com/p/tyr/>
* <http://dev.laubenberger.net/tyr/>
*
* Contact information:
* Stefan Laubenberger
* Bullingerstrasse 53
* CH-8004 Zuerich
*
* <http://www.laubenberger.net>
*
* <laubenberger@gmail.com>
*/
package net.laubenberger.tyr.model;

import java.io.File;
import java.math.BigDecimal;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

import net.laubenberger.bogatyr.helper.HelperLog;
import net.laubenberger.bogatyr.helper.HelperNumber;
import net.laubenberger.bogatyr.helper.HelperObject;
import net.laubenberger.bogatyr.helper.HelperString;
import net.laubenberger.bogatyr.helper.HelperXml;
import net.laubenberger.bogatyr.misc.exception.RuntimeExceptionIsNull;
import net.laubenberger.bogatyr.misc.xml.adapter.MapAdapterString;
import net.laubenberger.bogatyr.model.ModelAbstract;
import net.laubenberger.bogatyr.view.swing.factory.FormatFactory;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* The implementation of the module data.
*
* @author Stefan Laubenberger
* @version 0.9.1 (20120530)
* @since 0.2.0
*/
@XmlRootElement(name = "moduleData")
public class ModuleDataImpl extends ModelAbstract implements ModuleData {
  private static final long serialVersionUID = -6866025537131752531L;

  private static final Logger log = LoggerFactory.getLogger(ModuleDataImpl.class);

  private Map<String, String> data = new HashMap<String, String>();

  private final DateFormat df = FormatFactory.createDateFormat(FormatFactory.PATTERN_DATE_ISO8601);

  public ModuleDataImpl() {
    super();
    if (log.isTraceEnabled()) log.trace(HelperLog.constructor());
  }

//  public ModuleDataImpl(final Map<String, String> data, final UUID uuid, final Map<String, String> mapTag) {
//    super(uuid, mapTag);
//    if (log.isTraceEnabled()) log.trace(HelperLog.constructor(data, uuid, mapTag));
//
//    this.data = data;
//  }

  /*
   * Overridden methods
   */

  @Override
  public int hashCode() {
    final int prime = 31;
    int result = super.hashCode();
    result = prime * result + ((null == data) ? 0 : data.hashCode());
    result = prime * result + ((null == df) ? 0 : df.hashCode());
    return result;
  }

  @Override
  public boolean equals(final Object obj) {
    if (this == obj) return true;
    if (!super.equals(obj)) return false;
    if (getClass() != obj.getClass()) return false;
    final ModuleDataImpl other = (ModuleDataImpl) obj;
    if (null == data) {
      if (null != other.data) return false;
    } else if (!data.equals(other.data)) return false;
    if (null == df) {
      if (null != other.df) return false;
    } else if (!df.equals(other.df)) return false;
    return true;
  }
 
  @Override
  public String toString() {
    return getClass().getName() + "[instantiated=" + getInstantiated() //$NON-NLS-1$
      ", isNotifyEnabled=" + isNotifyEnabled() + //$NON-NLS-1$
      ", uuid=" + getUUID() //$NON-NLS-1$
      ", mapTag=" + getTags() //$NON-NLS-1$
      ", data=" + data +  //$NON-NLS-1$
      ']';
  }

 
  /*
   * Implemented methods
   */

  @Override
  @XmlElement
  @XmlJavaTypeAdapter(MapAdapterString.class)
  public Map<String, String> getData() {
    if (log.isDebugEnabled()) log.debug(HelperLog.methodStart());

    if (log.isDebugEnabled()) log.debug(HelperLog.methodExit(data));
    return data;
  }

  @Override
  public void setData(final Map<String, String> data) {
    if (log.isDebugEnabled()) log.debug(HelperLog.methodStart(data));

    if (!HelperObject.isEquals(data, this.data)) {
      this.data = data;
      setChanged();
      notifyObservers(MEMBER_DATA);
    }

    this.data = data;

    if (log.isDebugEnabled()) log.debug(HelperLog.methodExit());
  }

  @Override
  public void addValue(final String key, final String value) {
    if (log.isDebugEnabled()) log.debug(HelperLog.methodStart(key, value));
    if (null == key) {
      throw new RuntimeExceptionIsNull("key"); //$NON-NLS-1$
    }

    if (null == data) {
      data = new HashMap<String, String>();
    }
    data.put(key, value);
    setChanged();
    notifyObservers(METHOD_ADD_VALUE);

    if (log.isDebugEnabled()) log.debug(HelperLog.methodExit());
  }

  @Override
  public void addValue(final String key, final Boolean value) {
    if (log.isDebugEnabled()) log.debug(HelperLog.methodStart(key, value));

    addValue(key, Boolean.toString(value));

    if (log.isDebugEnabled()) log.debug(HelperLog.methodExit());
  }

  @Override
  public void addValue(final String key, final Number value) {
    if (log.isDebugEnabled()) log.debug(HelperLog.methodStart(key, value));

    addValue(key, value.toString());

    if (log.isDebugEnabled()) log.debug(HelperLog.methodExit());
  }

  @Override
  public void addValue(final String key, final File value) {
    if (log.isDebugEnabled()) log.debug(HelperLog.methodStart(key, value));

    addValue(key, value.getAbsolutePath());

    if (log.isDebugEnabled()) log.debug(HelperLog.methodExit());
  }

  @Override
  public void addValue(final String key, final URL value) {
    if (log.isDebugEnabled()) log.debug(HelperLog.methodStart(key, value));

    addValue(key, value.toString());

    if (log.isDebugEnabled()) log.debug(HelperLog.methodExit());
  }

  @Override
  public void addValue(final String key, final Date value) {
    if (log.isDebugEnabled()) log.debug(HelperLog.methodStart(key, value));

    addValue(key, df.format(value));

    if (log.isDebugEnabled()) log.debug(HelperLog.methodExit());
  }

  @Override
  public void removeValue(final String key) {
    if (log.isDebugEnabled()) log.debug(HelperLog.methodStart(key));
    if (null == key) {
      throw new RuntimeExceptionIsNull("key"); //$NON-NLS-1$
    }

    if (null != data) {
      data.remove(key);
      setChanged();
      notifyObservers(METHOD_REMOVE_VALUE);
    }

    if (log.isDebugEnabled()) log.debug(HelperLog.methodExit());
  }

  @Override
  public String getString(final String key) {
    if (log.isDebugEnabled()) log.debug(HelperLog.methodStart(key));
    if (null == key) {
      throw new RuntimeExceptionIsNull("key"); //$NON-NLS-1$
    }

    String result = null;
    if (null != data) {
      result = data.get(key);
    }

    if (log.isDebugEnabled()) log.debug(HelperLog.methodExit(result));
    return result;
  }

  @Override
  public Boolean getBoolean(final String key) {
    if (log.isDebugEnabled()) log.debug(HelperLog.methodStart(key));

    final String value = getString(key);
    final Boolean result = null == value ? null : Boolean.parseBoolean(value);

    if (log.isDebugEnabled()) log.debug(HelperLog.methodExit(result));
    return result;
  }

  @Override
  public BigDecimal getNumber(final String key) {
    if (log.isDebugEnabled()) log.debug(HelperLog.methodStart(key));

    final BigDecimal result = HelperNumber.getNumber(getString(key));

    if (log.isDebugEnabled()) log.debug(HelperLog.methodExit(result));
    return result;
  }

  @Override
  public File getFile(final String key) {
    if (log.isDebugEnabled()) log.debug(HelperLog.methodStart(key));

    final String value = getString(key);
    final File result = null == value ? null : new File(value);

    if (log.isDebugEnabled()) log.debug(HelperLog.methodExit(result));
    return result;
  }

  @Override
  public URL getURL(final String key) {
    if (log.isDebugEnabled()) log.debug(HelperLog.methodStart(key));

    final String value = getString(key);
    URL result = null;

    if (null != value) {
      try {
        result = new URL(value);
      } catch (MalformedURLException ex) {
        if (log.isInfoEnabled()) log.info("URL invalid", ex); //$NON-NLS-1$
      }
    }
    if (log.isDebugEnabled()) log.debug(HelperLog.methodExit(result));
    return result;
  }

  @Override
  public Date getDate(final String key) {
    if (log.isDebugEnabled()) log.debug(HelperLog.methodStart(key));

    Date result = null;
    try {
      result = df.parse(getString(key));
    } catch (ParseException ex) {
      if (log.isInfoEnabled()) log.info("Date invalid", ex); //$NON-NLS-1$
    }

    if (log.isDebugEnabled()) log.debug(HelperLog.methodExit(result));
    return result;
  }

  @Override
  public void addValue(final String key, final Object value) {
    if (log.isDebugEnabled()) log.debug(HelperLog.methodStart(key, value));
    if (null == key) {
      throw new RuntimeExceptionIsNull("key"); //$NON-NLS-1$
    }
    if (null == value) {
      throw new RuntimeExceptionIsNull("value"); //$NON-NLS-1$
    }

    try {
      addValue(key, HelperXml.serialize(value));
    } catch (Exception ex) {
      log.error("Could not serialize the value for the key " + HelperString.quote(key), ex); //$NON-NLS-1$
    }

    if (log.isDebugEnabled()) log.debug(HelperLog.methodExit());
  }

  @Override
  public <T> T getValue(final String key, final Class<T> clazz) {
    if (log.isDebugEnabled()) log.debug(HelperLog.methodStart(key, clazz));
    if (null == clazz) {
      throw new RuntimeExceptionIsNull("clazz"); //$NON-NLS-1$
    }

    T result = null;
    final String value = getString(key);

    if (null != value) {
      try {
        result = HelperXml.deserialize(value, clazz);
      } catch (Exception ex) {
        log.error("Could not deserialize the value for the key and class", ex); //$NON-NLS-1$
      }
    }

    if (log.isDebugEnabled()) log.debug(HelperLog.methodExit());
    return result;
  }

  /*
   * Inner classes
   */

  public static class XmlAdapterModuleData extends javax.xml.bind.annotation.adapters.XmlAdapter<ModuleDataImpl, ModuleData> {

    @Override
    public ModuleDataImpl marshal(final ModuleData model) {
      return (ModuleDataImpl) model;
    }

    @Override
    public ModuleData unmarshal(final ModuleDataImpl model) {
      return model;
    }
  }
}
TOP

Related Classes of net.laubenberger.tyr.model.ModuleDataImpl$XmlAdapterModuleData

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.