Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /* 
   * Copyright (C) 2012 FoxLabs
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   * http://www.apache.org/licenses/LICENSE-2.0
   * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.foxlabs.validation.path;
 
 
This iterator used by ValidationException to iterate over all violations hierarchy and allows to obtain path of the current violation.

 
 public final class PathIterator implements Iterator<ViolationException> {
    
    
Next violation.
 
     private ViolationException next;
    
    
Last violation.
 
     private ViolationException last;
    
    
Current iterator.
 
     private Iterator<ViolationExceptioniterator;
    
    
List of parent iterators.
 
     private final LinkedList<ItrNodeiterators;
    
    
Violations path.
 
     private final LinkedList<ViolationExceptionpath;
    
    
Formatter to be used for formatting violation nodes on the path.
 
     private final NodeFormatter formatter;
    
    
Constructs a new PathIterator with the specified violations hierarhy and path separator.

Parameters:
violations Violations hierarchy.
formatter Formatter to be used for formatting violation nodes on the path.
 
     public PathIterator(ValidationException violationsNodeFormatter formatter) {
         this. = violations.getRootViolations().iterator();
         this. = new LinkedList<ItrNode>();
         this. = new LinkedList<ViolationException>();
         this. = formatter;
     }
    
    
Determines if the iteration has more elements.

Returns:
true if the iteration has more elements; false otherwise.
 
     @Override
     public boolean hasNext() {
         if ( == null) {
             if (.hasNext()) {
                  = .next();
             } else {
                 if (.isEmpty()) 
                     return false;
                 ItrNode node = .removeLast();
                  = node.iterator;
                 if (node.cascade)
                     .removeLast();
                return hasNext();
            }
            Throwable cause = .getCause();
            while (!(cause == null || cause instanceof ValidationException))
                cause = cause.getCause();
            if (cause instanceof ValidationException) {
                boolean cascade = ((ValidationExceptioncause).isCascade();
                .addLast(new ItrNode(cascade));
                if (cascade)
                    .addLast();
                 = ((ValidationExceptioncause).getRootViolations().iterator();
                 = null;
                return hasNext();
            }
        }
        return true;
    }
    
    
Returns next violation in the hierarhy.

Returns:
Next violation in the hierarhy.
    @Override
    public ViolationException next() {
        if (hasNext()) {
             = ;
             = null;
            return ;
        }
        throw new NoSuchElementException();
    }
    
    
Returns array of violation nodes of the current path.

Returns:
Array of violation nodes of the current path.
    public ViolationException[] nodes() {
        if ( == null)
            throw new NoSuchElementException();
        ViolationException[] nodes = new ViolationException[.size() + 1];
        .toArray(nodes);
        nodes[nodes.length - 1] = ;
        return nodes;
    }
    
    
Returns path of the current violation.

Returns:
Path of the current violation.
    public String path() {
        StringBuilder buf = new StringBuilder();
        ViolationException[] nodes = nodes();
        if (nodes[0].getElementType() != null)
            .appendNode(nodes[0], buf);
        for (int i = 1; i < nodes.lengthi++) {
            if (nodes[i].getElementType() != null) {
                .appendSeparator(buf);
                .appendNode(nodes[i], buf);
            }
        }
        return buf.toString();
    }
    
    
Unsupported operation.
    @Override
    public void remove() {
        throw new UnsupportedOperationException();
    }
    
    // ItrNode
    
    
Iterator node.

Author(s):
Fox Mulder
    private static final class ItrNode {
        
        
Iterator of violations.
        private final Iterator<ViolationExceptioniterator;
        
        
Determines if iterator is over result of cascade validation.
        private final boolean cascade;
        
        
Constructs a new ItrNode with the specified iterator and cascade flag.

Parameters:
iterator Iterator of violations.
cascade Determines if iterator is over result of cascade validation.
        private ItrNode(Iterator<ViolationExceptioniteratorboolean cascade) {
            this. = iterator;
            this. = cascade;
        }
        
    }
    
New to GrepCode? Check out our FAQ X