package org.opengroupware.jope.foundation;

import java.util.Map;

/*
 * NSException
 * 
 * A runtime exception subclass which provides a bit of Foundation convenience.
 */
public class NSException extends RuntimeException
  implements NSKeyValueCoding, NSKeyValueCodingAdditions
{
  private static final long serialVersionUID = 1L;
  
  public NSException() {
    super();
  }
  public NSException(String _reason) {
    super(_reason);
  }

  /* KVC */
  
  public void takeValueForKey(Object _value, String _key) {
    NSKeyValueCoding.DefaultImplementation.takeValueForKey(this, _value, _key);
  }
  public Object valueForKey(String _key) {
    return NSKeyValueCoding.DefaultImplementation.valueForKey(this, _key);
  }
  
  public void takeValueForKeyPath(Object _value, String _keyPath) {
    NSKeyValueCodingAdditions.DefaultImplementation.
      takeValueForKeyPath(this, _value, _keyPath);
  }
  public Object valueForKeyPath(String _keyPath) {
    return NSKeyValueCodingAdditions.DefaultImplementation.
             valueForKeyPath(this, _keyPath);    
  }

  public Object handleQueryWithUnboundKey(String _key) {
    return null;
  }
  public void handleTakeValueForUnboundKey(Object _value, String _key) {
  }

  public void takeValuesFromDictionary(Map<String, Object> _map) {
    NSKeyValueCodingAdditions.DefaultImplementation
      .takeValuesFromDictionary(this, _map);
    
  }
  public Map<String, Object> valuesForKeys(String[] _keys) {
    return NSKeyValueCodingAdditions.DefaultImplementation.
      valuesForKeys(this, _keys);
  }
  
  /* description */
  
  public void appendAttributesToDescription(StringBuffer _d) {
    /* this is what should be overridden by subclasses */
    String s = this.getMessage();
    if (s != null && s.length() > 0)
      _d.append(" " + s);
  }
  
  public String toString() {
    StringBuffer sb = new StringBuffer(256);
    
    sb.append("<");
    sb.append(this.getClass().getName());
    // TODO: add some reference-id
    sb.append(":");
    
    this.appendAttributesToDescription(sb);
    sb.append(">");
    return sb.toString();
  }
}
