- java.lang.Object
-
- com.machinezoo.pushmode.dom.DomContent
-
- com.machinezoo.pushmode.dom.DomContainer
-
- com.machinezoo.pushmode.dom.DomFragment
-
- All Implemented Interfaces:
-
Cloneable
@NoTests public final class DomFragment extends DomContainer
Fragment of PushMode DOM tree. Fragment can have children just likeDomElement
, but it has no attributes nor other element properties. It's a thin extension ofDomContainer
.Fragment can be added to any
DomContainer
, including anotherDomFragment
. When added, the fragment is automatically inlined. Fragment thus cannot appear as a child ofDomElement
or anotherDomFragment
.It is recommended that application methods take and return
DomContent
instead ofDomElement
, which enables methods to pass around any combination of elements and plain text asDomFragment
, including an empty fragment, instead of just a singleDomElement
.
-
-
Constructor Summary
Constructors Constructor Description DomFragment()
Creates an empty DOM fragment.DomFragment(DomFragment other)
Creates deep mutable copy of the DOM fragment.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description DomFragment
add(DomContent child)
Adds new child node to this fragment.DomFragment
add(String text)
Adds literal text to this fragment Consecutive text nodes will be concatenated.<C extends DomContent>
DomFragmentadd(Collection<C> children)
Adds all nodes in aCollection
to this fragment.<C extends DomContent>
DomFragmentadd(Stream<C> children)
Adds all nodes in aStream
to this fragment.DomFragment
assign(DomFragment other)
Replaces contents of this fragment with contents of another fragment.DomFragment
clone()
Creates mutable deep clone of thisDomFragment
.boolean
equals(Object object)
Compares content of two fragments.DomFragment
freeze()
Protects this fragment from further modification.int
hashCode()
Computes hash code of this fragment.static DomFragment
join(DomContent separator, Iterable<? extends DomContent> items)
static DomFragment
join(String separator, Iterable<? extends DomContent> items)
-
Methods inherited from class com.machinezoo.pushmode.dom.DomContainer
children, descendants, element, elements, elements, rawChildCount, rawChildren, rawChildren, rawFreeze, text
-
Methods inherited from class com.machinezoo.pushmode.dom.DomContent
toString
-
-
-
-
Constructor Detail
-
DomFragment
public DomFragment()
Creates an empty DOM fragment.
-
DomFragment
public DomFragment(DomFragment other)
Creates deep mutable copy of the DOM fragment. Calling this constructor is equivalent to callingclone()
.- Parameters:
-
other
- fragment to copy - See Also:
-
clone()
-
-
Method Detail
-
clone
public DomFragment clone()
Creates mutable deep clone of thisDomFragment
. All child nodes are cloned recursively. The clone is completely independent of this fragment.- Specified by:
-
clone
in classDomContainer
- Returns:
- deep mutable clone
- See Also:
-
DomFragment(DomFragment)
-
assign
public DomFragment assign(DomFragment other)
Replaces contents of this fragment with contents of another fragment. Since fragment is merely a list of child nodes, these child nodes are copied from the source fragment into this fragment. Previous content of this fragment is discarded.Most of the DOM API consists of builder-like API to construct DOM trees. When DOM tree must be modified, it is recommended to completely rebuild it, perhaps using bits of the old tree in the process, and return the new tree. However, it is sometimes not possible to change the returned pointer and in those cases the fragment must be actually modified. Application should build new fragment as usual and then use this method to replace contents of the original fragment.
- Parameters:
-
other
- source fragment to be assigned to this fragment - Returns:
-
this
- Throws:
-
IllegalStateException
- if this fragment is frozen - See Also:
-
clone()
,DomFragment(DomFragment)
-
equals
public boolean equals(Object object)
Compares content of two fragments. If the supplied object is notDomFragment
, this method returns false. Equality is unaffected by whether the fragments are frozen or not. Children of both fragments are compared recursively by calling theirDomContent.equals(Object)
methods.- Overrides:
-
equals
in classDomContainer
- Parameters:
-
object
- object to compare this fragment with - Returns:
-
true
if the two fragments are equal,false
otherwise
-
hashCode
public int hashCode()
Computes hash code of this fragment. It is computed by combining hash codes of all children. Hash code is unaffected by whether the fragment is frozen or not.- Overrides:
-
hashCode
in classDomContainer
- Returns:
- fragment's hash code
-
add
public DomFragment add(DomContent child)
Adds new child node to this fragment. Fragments will be inlined,null
s ignored, and text concatenated.- Overrides:
-
add
in classDomContainer
- Parameters:
-
child
- node to add (ignored ifnull
) - Returns:
-
this
- Throws:
-
IllegalStateException
- if this element or fragment is frozen - See Also:
-
add(String)
,add(Collection)
,add(Stream)
-
add
public <C extends DomContent> DomFragment add(Collection<C> children)
Adds all nodes in aCollection
to this fragment. Fragments will be inlined,null
s ignored, and text concatenated.- Overrides:
-
add
in classDomContainer
- Type Parameters:
-
C
- item type - Parameters:
-
children
- collection of child nodes to add (ignored ifnull
) - Returns:
-
this
- Throws:
-
IllegalStateException
- if this element or fragment is frozen - See Also:
-
add(DomContent)
-
add
public <C extends DomContent> DomFragment add(Stream<C> children)
Adds all nodes in aStream
to this fragment. Fragments will be inlined,null
s ignored, and text concatenated.- Overrides:
-
add
in classDomContainer
- Type Parameters:
-
C
- item type - Parameters:
-
children
-Stream
of child nodes to add (ignored ifnull
) - Returns:
-
this
- Throws:
-
IllegalStateException
- if this element or fragment is frozen - See Also:
-
add(DomContent)
-
add
public DomFragment add(String text)
Adds literal text to this fragment Consecutive text nodes will be concatenated.- Overrides:
-
add
in classDomContainer
- Parameters:
-
text
- text to add (ignored ifnull
or empty) - Returns:
-
this
- Throws:
-
IllegalStateException
- if this element or fragment is frozen - See Also:
-
add(DomContent)
-
freeze
public DomFragment freeze()
Protects this fragment from further modification. For more information, use cases, and thread safety, seeDomContent.freeze()
.- Overrides:
-
freeze
in classDomContainer
- Returns:
-
this
- See Also:
-
DomContent.freeze()
-
join
@NoDocs @DraftApi("separate overloads for Collection and Stream") public static DomFragment join(DomContent separator, Iterable<? extends DomContent> items)
-
join
@NoDocs @DraftApi("separate overloads for Collection and Stream") public static DomFragment join(String separator, Iterable<? extends DomContent> items)
-
-