| 1 |
/* |
| 2 |
* |
| 3 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 4 |
* or more contributor license agreements. See the NOTICE file |
| 5 |
* distributed with this work for additional information |
| 6 |
* regarding copyright ownership. The ASF licenses this file |
| 7 |
* to you under the Apache License, Version 2.0 (the |
| 8 |
* "License"); you may not use this file except in compliance |
| 9 |
* with the License. You may obtain a copy of the License at |
| 10 |
* |
| 11 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 12 |
* |
| 13 |
* Unless required by applicable law or agreed to in writing, |
| 14 |
* software distributed under the License is distributed on an |
| 15 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 |
* KIND, either express or implied. See the License for the |
| 17 |
* specific language governing permissions and limitations |
| 18 |
* under the License. |
| 19 |
* |
| 20 |
*/ |
| 21 |
|
| 22 |
package org.apache.qpid.proton.codec.impl; |
| 23 |
|
| 24 |
abstract class AbstractElement<T> implements Element<T> |
| 25 |
{ |
| 26 |
private Element _parent; |
| 27 |
private Element _next; |
| 28 |
private Element _prev; |
| 29 |
|
| 30 |
AbstractElement(Element parent, Element prev) |
| 31 |
{ |
| 32 |
_parent = parent; |
| 33 |
_prev = prev; |
| 34 |
} |
| 35 |
|
| 36 |
protected boolean isElementOfArray() |
| 37 |
{ |
| 38 |
return _parent instanceof ArrayElement && !(((ArrayElement)parent()).isDescribed() && this == _parent.child()); |
| 39 |
} |
| 40 |
|
| 41 |
@Override |
| 42 |
public Element next() |
| 43 |
{ |
| 44 |
// TODO |
| 45 |
return _next; |
| 46 |
} |
| 47 |
|
| 48 |
@Override |
| 49 |
public Element prev() |
| 50 |
{ |
| 51 |
// TODO |
| 52 |
return _prev; |
| 53 |
} |
| 54 |
|
| 55 |
@Override |
| 56 |
public Element parent() |
| 57 |
{ |
| 58 |
// TODO |
| 59 |
return _parent; |
| 60 |
} |
| 61 |
|
| 62 |
@Override |
| 63 |
public void setNext(Element elt) |
| 64 |
{ |
| 65 |
_next = elt; |
| 66 |
} |
| 67 |
|
| 68 |
@Override |
| 69 |
public void setPrev(Element elt) |
| 70 |
{ |
| 71 |
_prev = elt; |
| 72 |
} |
| 73 |
|
| 74 |
@Override |
| 75 |
public void setParent(Element elt) |
| 76 |
{ |
| 77 |
_parent = elt; |
| 78 |
} |
| 79 |
|
| 80 |
@Override |
| 81 |
public Element replaceWith(Element elt) |
| 82 |
{ |
| 83 |
if (_parent != null) { |
| 84 |
elt = _parent.checkChild(elt); |
| 85 |
} |
| 86 |
|
| 87 |
elt.setPrev(_prev); |
| 88 |
elt.setNext(_next); |
| 89 |
elt.setParent(_parent); |
| 90 |
|
| 91 |
if (_prev != null) { |
| 92 |
_prev.setNext(elt); |
| 93 |
} |
| 94 |
if (_next != null) { |
| 95 |
_next.setPrev(elt); |
| 96 |
} |
| 97 |
|
| 98 |
if (_parent != null && _parent.child() == this) { |
| 99 |
_parent.setChild(elt); |
| 100 |
} |
| 101 |
|
| 102 |
return elt; |
| 103 |
} |
| 104 |
|
| 105 |
@Override |
| 106 |
public String toString() |
| 107 |
{ |
| 108 |
return String.format("%s[%h]{parent=%h, prev=%h, next=%h}", |
| 109 |
this.getClass().getSimpleName(), |
| 110 |
System.identityHashCode(this), |
| 111 |
System.identityHashCode(_parent), |
| 112 |
System.identityHashCode(_prev), |
| 113 |
System.identityHashCode(_next)); |
| 114 |
} |
| 115 |
|
| 116 |
abstract String startSymbol(); |
| 117 |
|
| 118 |
abstract String stopSymbol(); |
| 119 |
|
| 120 |
@Override |
| 121 |
public void render(StringBuilder sb) |
| 122 |
{ |
| 123 |
if (canEnter()) { |
| 124 |
sb.append(startSymbol()); |
| 125 |
Element el = child(); |
| 126 |
boolean first = true; |
| 127 |
while (el != null) { |
| 128 |
if (first) { |
| 129 |
first = false; |
| 130 |
} else { |
| 131 |
sb.append(", "); |
| 132 |
} |
| 133 |
el.render(sb); |
| 134 |
el = el.next(); |
| 135 |
} |
| 136 |
sb.append(stopSymbol()); |
| 137 |
} else { |
| 138 |
sb.append(getDataType()).append(" ").append(getValue()); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
} |