📄 message.java
字号:
} /** * {@inheritDoc} * <p/> * Not provided because the namespace cannot be specified. */ public void add(MessageElement obj) { throw new UnsupportedOperationException("add() not supported"); } /** * {@inheritDoc} */ public void remove() { if (origModCount != Message.this.getMessageModCount()) { RuntimeException failure = new ConcurrentModificationException( Message.this + " concurrently modified. Iterator was made at mod " + origModCount); if (Logging.SHOW_SEVERE && LOG.isLoggable(Level.SEVERE)) { LOG.log(Level.SEVERE, Message.this + " concurrently modified. iterator mod=" + origModCount + " current mod=" + Message.this.getMessageModCount() + "\n" + getMessageModHistory(), failure); } throw failure; } if (null == current) { throw new IllegalStateException("no current element, call next() or previous()"); } ListIterator<element> elsPosition = Message.this.elements.listIterator(); ListIterator<MessageElement> nsPosition = namespaces.get(current.namespace).listIterator(); int currentPrevious = list.previousIndex(); // restart this iterator while (list.previousIndex() >= 0) { list.previous(); } // readvance to the current position, but track in ns list and master list while (list.previousIndex() < currentPrevious) { element anElement = list.next(); try { // advance to the same element in the master list. element anElsElement; do { anElsElement = elsPosition.next(); } while (anElement != anElsElement); // advance to the same element in the ns list. MessageElement anNsElement; if (current.namespace.equals(anElement.namespace)) { do { anNsElement = nsPosition.next(); } while (anElement.element != anNsElement); } } catch (NoSuchElementException ranOut) { RuntimeException failure = new ConcurrentModificationException( Message.this + " concurrently modified. Iterator was made at mod " + origModCount); if (Logging.SHOW_SEVERE && LOG.isLoggable(Level.SEVERE)) { LOG.log(Level.SEVERE, Message.this + " concurrently modified. iterator mod=" + origModCount + " current mod=" + Message.this.getMessageModCount() + "\n" + getMessageModHistory(), failure); } throw failure; } } elsPosition.remove(); nsPosition.remove(); list.remove(); origModCount = Message.this.incMessageModCount(); if (Logging.SHOW_FINER && LOG.isLoggable(Level.FINER)) { LOG.finer( "Removed " + current.namespace + "::" + current.element.getElementName() + "/" + current.element.getClass().getName() + "@" + current.element.hashCode() + " from " + Message.this); } current = null; } /** * {@inheritDoc} * <p/> * Replacement MessageElement will be in the same name space as the * replaced element. */ public void set(MessageElement obj) { if (origModCount != Message.this.getMessageModCount()) { RuntimeException failure = new ConcurrentModificationException( Message.this + " concurrently modified. "); if (Logging.SHOW_SEVERE && LOG.isLoggable(Level.SEVERE)) { LOG.log(Level.SEVERE, Message.this + " concurrently modified. iterator mod=" + origModCount + " current mod=" + Message.this.getMessageModCount() + "\n" + getMessageModHistory(), failure); } throw failure; } if (null == current) { throw new IllegalStateException("no current element, call next() or previous()"); } ListIterator<element> elsPosition = Message.this.elements.listIterator(); ListIterator<MessageElement> nsPosition = namespaces.get(current.namespace).listIterator(); int currentPrevious = list.previousIndex(); // restart this iterator while (list.previousIndex() >= 0) { list.previous(); } // readvance to the current position, but track in ns list and master list while (list.previousIndex() < currentPrevious) { element anElement = list.next(); try { // advance to the same element in the master list. element anElsElement; do { anElsElement = elsPosition.next(); } while (anElement != anElsElement); // advance to the same element in the ns list. MessageElement anNsElement; if (current.namespace.equals(anElement.namespace)) { do { anNsElement = nsPosition.next(); } while (anElement.element != anNsElement); } } catch (NoSuchElementException ranOut) { RuntimeException failure = new ConcurrentModificationException( Message.this + " concurrently modified. Iterator was made at mod " + origModCount); if (Logging.SHOW_SEVERE && LOG.isLoggable(Level.SEVERE)) { LOG.log(Level.SEVERE, Message.this + " concurrently modified. iterator mod=" + origModCount + " current mod=" + Message.this.getMessageModCount() + "\n" + getMessageModHistory(), failure); } throw failure; } } Message.element newCurrent = new Message.element(current.namespace, obj, null); elsPosition.set(newCurrent); nsPosition.set(obj); list.set(newCurrent); origModCount = Message.this.incMessageModCount(); if (Logging.SHOW_FINER && LOG.isLoggable(Level.FINER)) { LOG.finer( "Replaced " + current.namespace + "::" + current.element.getElementName() + "/" + current.element.getClass().getName() + "@" + current.element.hashCode() + " with " + newCurrent.namespace + "::" + newCurrent.element.getElementName() + "/" + newCurrent.element.getClass().getName() + "@" + newCurrent.element.hashCode() + " in " + Message.this); } current = newCurrent; } /** * return the namespace of the current element. * * @return String containing the name space of the current element. */ public String getNamespace() { if (null == current) { throw new IllegalStateException("no current element, call next() or previous()"); } return current.namespace; } /** * Return the signature element of the current element. * * @return The signature element of the current element. */ public MessageElement getSignature() { if (null == current) { throw new IllegalStateException("no current element, call next() or previous()"); } return (null != current.signature) ? current.signature : current.element.getSignature(); } } /** * Holds an element, its namespace and optionally an override signature * element. */ protected static class element { final String namespace; final MessageElement element; final MessageElement signature; element(String namespace, MessageElement element, MessageElement signature) { this.namespace = namespace; this.element = element; this.signature = signature; } } /** * Standard Constructor for messages. The default namespace will be the * empty string ("") */ public Message() { this("", false); } /** * Standard Constructor for messages. * * @param defaultNamespace the namespace which is assumed by methods which * do not require a namespace specification. */ protected Message(String defaultNamespace) { this(defaultNamespace, false); } /** * Standard Constructor for messages. * * @param defaultNamespace the namespace which is assumed by methods which * do not require a namespace specification. * @param clone If {@code true} then we are creating a clone. */ private Message(String defaultNamespace, boolean clone) { this.defaultNamespace = defaultNamespace; lineage.add(messagenumber.getAndIncrement()); if (LOG_MODIFICATIONS) { modHistory = new ArrayList<Throwable>(); incMessageModCount(); } if (!clone && GLOBAL_TRACKING_ELEMENT) { UUID tracking = UUIDFactory.newSeqUUID(); MessageElement trackingElement = new StringMessageElement("Tracking UUID", tracking.toString(), null); addMessageElement("jxta", trackingElement); } } /** * {@inheritDoc} * <p/> * Duplicates the Message. The returned duplicate is a real copy. It may * be freely modified without causing change to the originally cloned * message. * * @return Message a Message that is a copy of the original message */ @Override public Message clone() { Message clone = new Message(getDefaultNamespace(), true ); clone.lineage.addAll(lineage); clone.elements.addAll(elements); for (String aNamespace : namespaces.keySet()) { List<MessageElement> namespaceElements = namespaces.get(aNamespace); List<MessageElement> newNamespaceElements = new ArrayList<MessageElement>(namespaceElements.size()); newNamespaceElements.addAll(namespaceElements); clone.namespaces.put(aNamespace, newNamespaceElements); } if (Logging.SHOW_FINER && LOG.isLoggable(Level.FINER)) { LOG.finer("Created clone " + clone + " of " + this); } return clone; } /** * {@inheritDoc} * <p/> * Compare this Message against another. Returns {@code true} if all of the * elements are identical and in the same order. Message properties * (setProperty()/getProperty()) are not considered in the calculation. * * @param target The Message to compare against. * @return {@code true} if the elements are identical otherwise * {@code false}. */ @Override public boolean equals(Object target) { if (this == target) { return true; } if (target instanceof Message) { Message likeMe = (Message) target; ElementIterator myElements = getMessageElements(); ElementIterator itsElements = likeMe.getMessageElements(); while (myElements.hasNext()) { if (!itsElements.hasNext()) { return false; // it has fewer than i do. } MessageElement mine = myElements.next(); MessageElement its = itsElements.next(); if (!myElements.getNamespace().equals(itsElements.getNamespace())) { return false; // elements not in the same namespace } if (!mine.equals(its)) { return false; // content didnt match } } return (!itsElements.hasNext()); // ran out at the same time? } return false; // not a message } /** * {@inheritDoc} */ @Override public int hashCode() { int result = 0; Iterator<MessageElement> eachElement = getMessageElements(); while (eachElement.hasNext()) { MessageElement anElement = eachElement.next(); result += anElement.hashCode(); result *= 6037; // a prime } if (0 == result) { result = 1; } return result; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -