📄 message.java
字号:
throw failure; } current = (element) list.previous(); return current.element; } /** * {@inheritDoc} **/ public int previousIndex() { return list.previousIndex(); } /** * {@inheritDoc} * * <p/>Not provided because the namespace cannot be specified. **/ public void add( Object 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 (LOG.isEnabledFor(Level.ERROR)) { LOG.error( 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 elsPosition = Message.this.elements.listIterator(); ListIterator nsPosition = ((List)( 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 = (element) list.next(); try { // advance to the same element in the master list. element anElsElement; do { anElsElement = (element) elsPosition.next(); } while( anElement != anElsElement ); // advance to the same element in the ns list. MessageElement anNsElement; if( current.namespace.equals( anElement.namespace ) ) { do { anNsElement = (MessageElement) nsPosition.next(); } while( anElement.element != anNsElement ); } } catch ( NoSuchElementException ranOut ) { RuntimeException failure = new ConcurrentModificationException( Message.this + " concurrently modified. Iterator was made at mod " + origModCount ); if (LOG.isEnabledFor(Level.ERROR)) { LOG.error( 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 (LOG.isEnabledFor(Level.DEBUG)) { LOG.debug("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( Object obj ) { if( origModCount != Message.this.getMessageModCount() ) { RuntimeException failure = new ConcurrentModificationException( Message.this + " concurrently modified. Iterator was made at mod " + origModCount ); if (LOG.isEnabledFor(Level.ERROR)) { LOG.error( Message.this + " concurrently modified. iterator mod=" + origModCount + " current mod=" + Message.this.getMessageModCount() + "\n" + getMessageModHistory(), failure ); } throw failure; } if( !(obj instanceof MessageElement) ) { throw new IllegalStateException( "replacement must be a MessageElement" ); } if( null == current ) { throw new IllegalStateException( "no current element, call next() or previous()" ); } ListIterator elsPosition = Message.this.elements.listIterator(); ListIterator nsPosition = ((List)( 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 = (element) list.next(); try { // advance to the same element in the master list. element anElsElement; do { anElsElement = (element) elsPosition.next(); } while( anElement != anElsElement ); // advance to the same element in the ns list. MessageElement anNsElement; if( current.namespace.equals( anElement.namespace ) ) { do { anNsElement = (MessageElement) nsPosition.next(); } while( anElement.element != anNsElement ); } } catch ( NoSuchElementException ranOut ) { RuntimeException failure = new ConcurrentModificationException( Message.this + " concurrently modified. Iterator was made at mod " + origModCount ); if (LOG.isEnabledFor(Level.ERROR)) { LOG.error( 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, (MessageElement) obj ); elsPosition.set( newCurrent ); nsPosition.set( obj ); list.set( newCurrent ); origModCount = Message.this.incMessageModCount(); if (LOG.isEnabledFor(Level.DEBUG)) { LOG.debug("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; } } /** * holds an element and its namespace **/ protected static class element { String namespace; MessageElement element; element( String namespace, MessageElement element ) { this.namespace = namespace; this.element = element; } } /** * Returns the next message number in sequence. * * @return the next message number in sequence. **/ protected static int getNextMessageNumber() { synchronized( Message.class ) { return messagenumber++; } } /** * Standard Constructor for messages. The default namespace will be the * empty string ("") **/ public Message() { this( "" ); if( GLOBAL_TRACKING_ELEMENT ) { UUID tracking = UUIDFactory.newSeqUUID(); MessageElement trackingElement = new StringMessageElement( "Tracking UUID", tracking.toString(), null ); addMessageElement( "jxta", trackingElement ); } } /** * Standard Constructor for messages. * * @param defaultNamespace the namespace which is assumed when calls are * made that don't include a namespace specification. **/ protected Message(String defaultNamespace) { this.defaultNamespace = defaultNamespace; lineage.add( new Integer( getNextMessageNumber() ) ); if ( LOG_MODIFICATIONS ) { modHistory = new ArrayList(); incMessageModCount(); } } /** * {@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 **/ public Object clone() { Message clone = new Message( getDefaultNamespace() ); clone.lineage.addAll( lineage ); clone.elements.addAll( elements ); Iterator eachNamespace = namespaces.keySet().iterator(); while( eachNamespace.hasNext() ) { String aNamespace = (String) eachNamespace.next(); List namespaceElements = (List) namespaces.get( aNamespace ); List newNamespaceElements = new ArrayList( namespaceElements.size() ); newNamespaceElements.addAll( namespaceElements ); clone.namespaces.put( aNamespace, newNamespaceElements ); } if (LOG.isEnabledFor(Level.DEBUG)) { LOG.debug("Created clone " + clone + " of " + this ); } return clone; } /** * {@inheritDoc} * * <p/> Compare this Message against another. Returns 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 to compare against. * @return boolean true if the elements are identical. **/ 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 = (MessageElement) myElements.next(); MessageElement its = (MessageElement) 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} **/ public int hashCode() { int result = 0; Iterator eachElement = getMessageElements(); while( eachElement.hasNext() ) { MessageElement anElement = (MessageElement) eachElement.next(); result += anElement.hashCode(); result *= 6037; // a prime } if( 0 == result ) { result = 1; } return result; } /** * {@inheritDoc} * * <p/> Displays the lineage of the message including its own message number. * This is useful primarily for debugging purposes. **/ public String toString() { StringBuffer toString = new StringBuffer( 128 ); toString.append( getClass().getName() ); toString.append( '@' ); toString.append( super.hashCode() ); toString.append( '(' ); toString.append( modCount ); toString.append( "){" );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -