/*
 *    Copyright (c) 2013, University of Toronto.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License"); you may
 *    not use this file except in compliance with the License. You may obtain
 *    a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 *    License for the specific language governing permissions and limitations
 *    under the License.
 */
package edu.toronto.cs.xcurator.model;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * Represents an attribute in a schema.
 *
 * @author Soheil Hassas Yeganeh <soheil@cs.toronto.edu>
 */
public class Attribute {
	// Eric: Moved member variables to top to be consistent
	// with other xcurator classes
	String name;
  String path;
  Schema parent;
  boolean key;

  Set<String> typeURIs = new HashSet<String>();
  Map<SchemaInstance, Set<String>> instanceMap;
  Map<String, Set<SchemaInstance>> reverseInstanceMap;
	
  public Attribute(Schema parent, String name, String path, boolean key) {
    super();
    this.name = name;
    this.path = path;
    this.parent = parent;
    this.key = key;

    instanceMap = new HashMap<SchemaInstance, Set<String>>();
    reverseInstanceMap = new HashMap<String, Set<SchemaInstance>>();
  }

  public void addInstance(AttributeInstance instance) {
    Set<String> attributes =
        instanceMap.get(instance.schemaInstance);
    if (attributes == null) {
      attributes = new HashSet<String>();
      instanceMap.put(instance.schemaInstance, attributes);
    }
    attributes.add(instance.content);

    Set<SchemaInstance> schemas = reverseInstanceMap.get(instance.content);
    if (schemas == null) {
    	schemas = new HashSet<SchemaInstance>();
      reverseInstanceMap.put(instance.content, schemas);
    }
    schemas.add(instance.schemaInstance);
  }

  public boolean isOneToOne() {
    for (Map.Entry<SchemaInstance, Set<String>> entry :
        instanceMap.entrySet()) {
      if (entry.getValue().size() > 1) {
        return false;
      }
    }

    for (Map.Entry<String, Set<SchemaInstance>> entry :
        reverseInstanceMap.entrySet()) {
      if (entry.getValue().size() > 1) {
        return false;
      }
    }

    return true;
  }

  public void setParent(Schema parent) {
    this.parent = parent;
  }

  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public String getPath() {
    return path;
  }
  public void setPath(String path) {
    this.path = path;
  }

  public boolean isKey() {
    return key;
  }

  public void setKey(boolean key) {
    this.key = key;
  }

  public Schema getParent() {
    return parent;
  }

  @Override
  public String toString() {
    return "A@ " + name;
  }

  @Override
  public boolean equals(Object obj) {
    if (obj instanceof Attribute) {
      Attribute attr = (Attribute) obj;
      return attr.name.equals(this.name) && attr.parent.equals(this.parent) &&
          attr.path.equals(this.path);
    }
    return super.equals(obj);
  }

  @Override
  public int hashCode() {
    return name.hashCode();
  }

  public Set<String> getTypeURIs() {
    return typeURIs;
  }

  public void setTypeURIs(Set<String> typeURIs) {
    this.typeURIs = typeURIs;
  }
}

@@ -1130,22 +1130,33 @@
 ce, Set%3C
-String
+AttributeInstance
 %3E%3E insta
@@ -1173,38 +1173,41 @@
 Map%3CString, Set%3C
-Schema
+Attribute
 Instance%3E%3E rever
@@ -1452,22 +1452,33 @@
 ce, Set%3C
-String
+AttributeInstance
 %3E%3E();%0A  
@@ -1516,38 +1516,41 @@
 Map%3CString, Set%3C
-Schema
+Attribute
 Instance%3E%3E();%0A  
@@ -1616,22 +1616,33 @@
     Set%3C
-String
+AttributeInstance
 %3E attrib
@@ -1755,30 +1755,41 @@
 new HashSet%3C
-String
+AttributeInstance
 %3E();%0A      i
@@ -1878,50 +1878,25 @@
 ance
-.content);%0A%0A    Set%3CSchemaInstance%3E schema
+);%0A%0A    attribute
 s = 
@@ -1945,22 +1945,25 @@
     if (
-schema
+attribute
 s == nul
@@ -1972,22 +1972,25 @@
  %7B%0A    %09
-schema
+attribute
 s = new 
@@ -1993,30 +1993,33 @@
 new HashSet%3C
-Schema
+Attribute
 Instance%3E();
@@ -2066,22 +2066,25 @@
 ontent, 
-schema
+attribute
 s);%0A    
@@ -2093,36 +2093,24 @@
     
-schemas.add(instance.schemaI
+attributes.add(i
 nsta
@@ -2191,22 +2191,33 @@
 ce, Set%3C
-String
+AttributeInstance
 %3E%3E entry
@@ -2362,22 +2362,25 @@
 ng, Set%3C
-Schema
+Attribute
 Instance

Revert changes in Attribute.