sk.baka.ikslibs.interval
Class DOMIntervalSet

java.lang.Object
  extended by sk.baka.ikslibs.interval.DOMIntervalSet
All Implemented Interfaces:
Iterable<DOMInterval>

public final class DOMIntervalSet
extends Object
implements Iterable<DOMInterval>

An ordered set of disjunctive non-empty intervals. Mutable, thread unsafe.

References live instance of the document - the interval may become invalid when the document is changed.

Author:
Martin Vysny

Constructor Summary
DOMIntervalSet()
          Constructs an empty set.
DOMIntervalSet(DOMInterval interval)
          Constructs set with one item.
DOMIntervalSet(DOMIntervalSet other)
          Copy-constructor.
 
Method Summary
 void clear()
          Removes all intervals from this set.
 DocumentFragment cloneContents(Node factoryReference)
           The 'copy' operation.
 DOMIntervalSet complement(Node root, IDManager idManager, MutableReference<List<IDSelectionAction>> actions)
          Inverts the interval set - all nodes that were not part of the set will become contents of the set and vice versa.
 boolean contains(DOMInterval interval)
          Checks if this set fully covers given interval.
 void deleteContents(boolean deleteEmptyNode)
          The 'delete' operation.
 boolean equals(Object obj)
           
 DocumentFragment extractContents(Node factoryReference, boolean deleteEmptyNode)
           The 'cut' operation.
 IntervalOrInsertPoint findIntervalOrInsertPoint(DomPointer ptr)
           Finds index of interval that contains given pointer, or an insert point where interval starting with given pointer would be inserted.
 Set<String> getContentIds(IDManager idManager)
          Returns set of IDs, covered (partially) by some interval(s).
 List<Node> getContents()
          Returns set of IDs, covered (partially) by some interval(s) contained in this interval set.
 List<DOMInterval> getIntervals()
          Returns an list of all intervals.
 int hashCode()
           
 DOMIntervalSet intersect(DOMInterval interval, MutableReference<DOMIntervalSet> diff)
          Intersects given interval with this set.
 DOMIntervalSet intersect(DOMIntervalSet other, MutableReference<DOMIntervalSet> diff)
          Intersects given set with this set.
 DOMIntervalSet invert(DOMInterval other)
          Inverts intervals covered by given interval.
 DOMIntervalSet invert(DOMIntervalSet other)
          Inverts intervals covered by the 'other' set.
 boolean isEmpty()
          Checks if this set covers at least one node (or some characters of some node).
 Iterator<DOMInterval> iterator()
           
 DOMIntervalSet subtract(DOMInterval interval, MutableReference<DOMIntervalSet> diff)
          Subtracts given interval to this interval set.
 DOMIntervalSet subtract(DOMIntervalSet other, MutableReference<DOMIntervalSet> diff)
          Subtracts all intervals from given set to this interval set.
 List<IDSelectionAction> toSelectionActions(boolean select, IDManager idManager)
          Returns this set as a list of IDSelectionAction elements.
 String toString()
           
 DOMIntervalSet union(DOMInterval interval, MutableReference<DOMIntervalSet> diff)
          Adds given interval to this interval set.
 DOMIntervalSet union(DOMIntervalSet other, MutableReference<DOMIntervalSet> diff)
          Adds all intervals from given set to this interval set.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

DOMIntervalSet

public DOMIntervalSet()
Constructs an empty set.


DOMIntervalSet

public DOMIntervalSet(DOMIntervalSet other)
Copy-constructor.

Parameters:
other - clone this interval set.

DOMIntervalSet

public DOMIntervalSet(DOMInterval interval)
Constructs set with one item.

Parameters:
interval - the interval.
Method Detail

iterator

public Iterator<DOMInterval> iterator()
Specified by:
iterator in interface Iterable<DOMInterval>

getIntervals

public List<DOMInterval> getIntervals()
Returns an list of all intervals. The intervals are sorted and not adjacent.

Returns:
unmodifiable list of intervals.

getContents

public List<Node> getContents()
Returns set of IDs, covered (partially) by some interval(s) contained in this interval set. This method repeatedly uses the DOMInterval.getContents() method, removing duplicate nodes. For details please look there.

Returns:
unmodifiable set of nodes partially covered by this set. Nodes are sorted in the document ordering (parent first, then its children).

getContentIds

public Set<String> getContentIds(IDManager idManager)
Returns set of IDs, covered (partially) by some interval(s).

Parameters:
idManager - the ID manager.
Returns:
unmodifiable set of IDs.

union

public DOMIntervalSet union(DOMInterval interval,
                            MutableReference<DOMIntervalSet> diff)
Adds given interval to this interval set. This will take O(log(|this|)) in worst case.

Parameters:
interval - interval to add. Adjacent nodes are merged automatically.
diff - receives a never-null set difference between new set and the old set. A list of intervals that would have been added to the old set in order to obtain the new set. This parameter may be null if you do not want to compute the diff set.
Returns:
this

union

public DOMIntervalSet union(DOMIntervalSet other,
                            MutableReference<DOMIntervalSet> diff)
Adds all intervals from given set to this interval set. This will take |other|.O(log(|this|)) in worst case.

Parameters:
other - set of intervals to add. Adjacent nodes are merged automatically.
diff - receives a never-null set difference between new set and the old set. A list of intervals that would have been added to the old set in order to obtain the new set. This parameter may be null if you do not want to compute the diff set.
Returns:
this

subtract

public DOMIntervalSet subtract(DOMInterval interval,
                               MutableReference<DOMIntervalSet> diff)
Subtracts given interval to this interval set. This will take O(log(|this|)) in worst case.

Parameters:
interval - interval to subtract.
diff - receives a never-null set difference between new set and the old set. A list of intervals that would have been removed from the old set in order to obtain the new set. This parameter may be null if you do not want to compute the diff set.
Returns:
this

subtract

public DOMIntervalSet subtract(DOMIntervalSet other,
                               MutableReference<DOMIntervalSet> diff)
Subtracts all intervals from given set to this interval set. This will take |other|.O(log(|this|)) in worst case.

Parameters:
other - set of intervals to add.
diff - receives a non-null set difference between new set and the old set. A list of intervals that would have been removed from the old set in order to obtain the new set. This parameter may be null if you do not want to compute the diff set.
Returns:
this

intersect

public DOMIntervalSet intersect(DOMIntervalSet other,
                                MutableReference<DOMIntervalSet> diff)
Intersects given set with this set. This will take |other|.O(log(|this|)) in worst case.

Parameters:
other - the set to intersect with.
diff - receives a non-null set difference between new set and the old set. A list of intervals that would have been removed from the old set in order to obtain the new set. This parameter may be null if you do not want to compute the diff set.
Returns:
this

intersect

public DOMIntervalSet intersect(DOMInterval interval,
                                MutableReference<DOMIntervalSet> diff)
Intersects given interval with this set. This will take O(log(|this|)) in the worst case.

Parameters:
interval - the set to intersect with.
diff - receives a non-null set difference between new set and the old set. A list of intervals that would have been removed from the old set in order to obtain the new set. This parameter may be null if you do not want to compute the diff set.
Returns:
this

contains

public boolean contains(DOMInterval interval)
Checks if this set fully covers given interval. This will take O(log(|this|)) in worst case.

Parameters:
interval - interval to check
Returns:
true if all content of given interval is fully covered by the set, false if some nodes (or parts of nodes) are outside the set.

complement

public DOMIntervalSet complement(Node root,
                                 IDManager idManager,
                                 MutableReference<List<IDSelectionAction>> actions)
Inverts the interval set - all nodes that were not part of the set will become contents of the set and vice versa.

Parameters:
root - if the interval is empty then the complement will contain all child nodes of this node. If the interval is not empty then this parameter is ignored and may be null - the complement will be computed in the context of the whole document.
idManager - the ID manager. If null then all actions will have null id.
actions - receives a non-null list of selection action that needs to be executed in order for the selection to be in sync with the set. This parameter may be null if you do not want to compute the action list.
Returns:
this

toSelectionActions

public List<IDSelectionAction> toSelectionActions(boolean select,
                                                  IDManager idManager)
Returns this set as a list of IDSelectionAction elements.

Parameters:
select - the value of the IDSelectionAction.select attribute for all returned instances.
idManager - the ID manager. If null then all actions will have null id.
Returns:
list of actions to execute when you want to display selection for the entire set. Never null, may be empty if this set is empty.

isEmpty

public boolean isEmpty()
Checks if this set covers at least one node (or some characters of some node).

Returns:
true if this set is empty, false if it contains at least one non-empty interval.

findIntervalOrInsertPoint

public IntervalOrInsertPoint findIntervalOrInsertPoint(DomPointer ptr)

Finds index of interval that contains given pointer, or an insert point where interval starting with given pointer would be inserted. This will take O(log(n)) in worst case.

Parameters:
ptr - pointer to check.
Returns:
instance of IntervalOrInsertPoint class providing information about the pointer.

equals

public boolean equals(Object obj)
Overrides:
equals in class Object

hashCode

public int hashCode()
Overrides:
hashCode in class Object

toString

public String toString()
Overrides:
toString in class Object

clear

public void clear()
Removes all intervals from this set.


deleteContents

public void deleteContents(boolean deleteEmptyNode)
The 'delete' operation. Deletes contents covered by this interval set. Calls Range.deleteContents() on each covered interval. Please note that the set and all its intervals becomes invalid and should not be used further.

Parameters:
deleteEmptyNode - if true then empty text/cdata/comment/attribute/PI nodes are removed when their content is completely covered by the interval. For example, DOMInterval.create(attr).deleteContents(false) will leave empty attribute while DOMInterval.create(attr).deleteContents(true) will remove the attribute aswell.

cloneContents

public DocumentFragment cloneContents(Node factoryReference)

The 'copy' operation. Clones contents of all intervals using the Range.cloneContents() and places extracted contents into a single fragment. Node.normalize() is called on the result fragment.

Attribute intervals are ignored.

Parameters:
factoryReference - if the interval set is empty then the empty result document fragment will be constructed using owner of this node. If the interval is not empty then this parameter is ignored and may be null.
Returns:
never-null document fragment, may be empty.

extractContents

public DocumentFragment extractContents(Node factoryReference,
                                        boolean deleteEmptyNode)

The 'cut' operation. Extracts contents of all intervals using the Range.extractContents() and places extracted contents into a single fragment. Node.normalize() is called on the result fragment.

Please note that the set and all its intervals becomes invalid and should not be used further.

Attribute intervals are ignored.

Parameters:
factoryReference - if the interval set is empty then the empty result document fragment will be constructed using owner of this node. If the interval is not empty then this parameter is ignored and may be null.
deleteEmptyNode - if true then empty text/cdata/comment/attribute/PI nodes are removed when their content is completely covered by the interval. For example, DOMInterval.create(attr).deleteContents(false) will leave empty attribute while DOMInterval.create(attr).deleteContents(true) will remove the attribute aswell.
Returns:
never-null document fragment, may be empty.

invert

public DOMIntervalSet invert(DOMIntervalSet other)
Inverts intervals covered by the 'other' set. Modifies this set to contain set of elements which are in one of either set, but not in both. Effectively performs a symmetric difference of these two sets.

Parameters:
other - inverts intervals present in this set. The set is not modified.
Returns:
this

invert

public DOMIntervalSet invert(DOMInterval other)
Inverts intervals covered by given interval. Modifies this set to contain set of elements which are in one of either set, but not in both. Effectively performs a symmetric difference of these two sets.

Parameters:
other - inverts intervals covered by this interval.
Returns:
this


Copyright © 2006 Martin Vysny - baka. All Rights Reserved.