package ch.ntb.inf.deep.config;

import ch.ntb.inf.deep.strings.HString;

public class Segment implements IAttributes {
	public Segment subSegments;
	private Segment lastSubSegment;
	public Segment next;
	
	HString name;
	int attributes = 0;
	int baseAddress = -1;
	int size = 0;
	int requiredSize = 0;
	int width = 0;
	
	public Segment(HString name){
		this.name = name;
	}
	
	public Segment(HString name, int baseAddress) {
		this.name = name;
		this.baseAddress = baseAddress;
	}
	
	public Segment(HString name, int baseAddress, int size) {
		this.name = name;
		this.baseAddress = baseAddress;
		this.size = size;
	}
	
	public Segment(HString name, int baseAddress, int size, int width) {
		this.name = name;
		this.baseAddress = baseAddress;
		this.size = size;
		this.width = width;
	}
	
	public Segment(HString name, int baseAddress, int size, int width, int attributes) {
		this.name = name;
		this.baseAddress = baseAddress;
		this.size = size;
		this.width = width;
		this.attributes = attributes;
	}
		
	public void setAttribute(int attributes) {
		this.attributes = attributes;
	}
	
	public void addAttributes(int attributes) {
		this.attributes |= attributes;
	}
	
	public void setAttributes(int attributes) {
		this.attributes = attributes;
	}
	
	public void removeAttributes(int attributes) {
		this.attributes &= ~attributes;
	}
	
	public void setBaseAddress(int baseAddress) {
		this.baseAddress = baseAddress;
	}
	
	public void setSize(int size) {
		this.size = size;
	}
	
	public void setWidth(int width) {
		this.width = width;
	}
	
	public void setEndAddress(int endAddress) {
		if(this.baseAddress >= 0) {
			this.size = endAddress - this.baseAddress;
		}
	}
	
	public int getBaseAddress() {
		return this.baseAddress;
	}
	
	public int getSize() {
		return this.size;
	}
	
	public int getWidth() {
		return width;
	}
	
	public HString getName(){
		return name;
	}
	
	public int getAttributes(){
		return attributes;
	}

	
	public boolean addSubSegment(Segment s) {
		if(s.width == this.width) {
			if(subSegments == null) {
				subSegments = s;
				lastSubSegment = subSegments;
			}
			else {
				lastSubSegment.next = s;
				lastSubSegment = lastSubSegment.next;
			}
			return true;
		}
		return false;
	}
	
	/**
	 * Add Size to requiredSize.
	 * @param size
	 */
	public void addToRequiredSize(int size){
		requiredSize += size;
	}
	
	
	/**
	 * @return the required size of the Segement.
	 */
	public int getRequiredSize(){
		return requiredSize;
	}
	public Segment getSubSegmentByName(HString name){
		int segHash = name.hashCode();
		Segment current = subSegments;
		while(current != null){
			if(current.name.hashCode() == segHash){
				if(current.name.equals(name)){
					return current;
				}
			}
			current = current.next;
		}
		return current;
	}
	
	public void println(int indentLevel){
		for(int i = indentLevel; i > 0; i--){
			System.out.print("  ");
		}
		System.out.println("Segment " + name.toString() + "{");
		for(int i = indentLevel + 1; i > 0; i--){
			System.out.print("  ");
		}
		System.out.print("attributes: 0x" + Integer.toHexString(attributes) + ", width: " + width );
		if(baseAddress != -1){
			System.out.print(", base: 0x" + Integer.toHexString(baseAddress));
		}
		if(size > 0){
			System.out.print(", size: 0x" + Integer.toHexString(size));
		}
		System.out.println(";");
		Segment current = subSegments;
		while(current != null){
			current.println(indentLevel + 1);
			current = current.next;
		}
		for(int i = indentLevel; i > 0; i--){
			System.out.print("  ");
		}
		System.out.println("}");
	}
}

@@ -154,14 +154,38 @@
 %0D%0A%09p
-rivate
+ublic Segment parent;%0D%0A%09public
  Seg
@@ -229,16 +229,39 @@
  next;%0D%0A
+%09public Segment prev;%0D%0A
 %09%0D%0A%09HStr
@@ -2125,24 +2125,44 @@
 gment s) %7B%0D%0A
+%09%09s.parent = this;%0D%0A
 %09%09if(s.width
@@ -2310,24 +2310,54 @@
 .next = s;%0D%0A
+%09%09%09%09s.prev = lastSubSegment;%0D%0A
 %09%09%09%09lastSubS

add div references