Coverage Report - org.eclipse.swtbot.swt.finder.utils.TreePath
 
Classes in this File Line Coverage Branch Coverage Complexity
TreePath
80%
36/45
75%
15/20
3.375
 
 1  
 /*******************************************************************************
 2  
  * Copyright (c) 2005, 2007 IBM Corporation and others.
 3  
  * All rights reserved. This program and the accompanying materials
 4  
  * are made available under the terms of the Eclipse Public License v1.0
 5  
  * which accompanies this distribution, and is available at
 6  
  * http://www.eclipse.org/legal/epl-v10.html
 7  
  *
 8  
  * Contributors:
 9  
  *     IBM Corporation - initial API and implementation
 10  
  *******************************************************************************/
 11  
 package org.eclipse.swtbot.swt.finder.utils;
 12  
 
 13  
 import java.util.Arrays;
 14  
 
 15  
 import org.eclipse.swtbot.swt.finder.utils.internal.Assert;
 16  
 
 17  1
 public final class TreePath {
 18  
 
 19  
         /**
 20  
          * Constant for representing an empty tree path.
 21  
          */
 22  1
         public static final TreePath        EMPTY        = new TreePath(new Object[0]);
 23  
 
 24  
         private Object[]                                segments;
 25  
 
 26  
         private int                                                hash;
 27  
 
 28  
         /**
 29  
          * Constructs a path identifying a leaf node in a tree.
 30  
          * 
 31  
          * @param segments path of elements to a leaf node in a tree, starting with the root element
 32  
          */
 33  513
         public TreePath(Object... segments) {
 34  513
                 Assert.isNotNull(segments);
 35  3787
                 for (int i = 0; i < segments.length; i++) {
 36  3274
                         Assert.isNotNull(segments[i]);
 37  
                 }
 38  513
                 this.segments = segments;
 39  513
         }
 40  
 
 41  
         /**
 42  
          * Returns the element at the specified index in this path.
 43  
          * 
 44  
          * @param index index of element to return
 45  
          * @return element at the specified index
 46  
          */
 47  
         public Object getSegment(int index) {
 48  1611
                 return segments[index];
 49  
         }
 50  
 
 51  
         /**
 52  
          * Returns the number of elements in this path.
 53  
          * 
 54  
          * @return the number of elements in this path
 55  
          */
 56  
         public int getSegmentCount() {
 57  512
                 return segments.length;
 58  
         }
 59  
 
 60  
         /**
 61  
          * Returns the last element in this path, or <code>null</code> if this path has no segments.
 62  
          * 
 63  
          * @return the last element in this path
 64  
          */
 65  
         public Object getLastSegment() {
 66  5
                 if (segments.length == 0) {
 67  1
                         return null;
 68  
                 }
 69  4
                 return segments[segments.length - 1];
 70  
         }
 71  
 
 72  
         public int hashCode() {
 73  
                 final int prime = 31;
 74  0
                 int result = 1;
 75  0
                 result = prime * result + hash;
 76  0
                 result = prime * result + Arrays.hashCode(segments);
 77  0
                 return result;
 78  
         }
 79  
 
 80  
         public boolean equals(Object obj) {
 81  2
                 if (this == obj)
 82  0
                         return true;
 83  2
                 if (obj == null)
 84  0
                         return false;
 85  2
                 if (getClass() != obj.getClass())
 86  0
                         return false;
 87  2
                 TreePath other = (TreePath) obj;
 88  2
                 if (hash != other.hash)
 89  0
                         return false;
 90  2
                 if (!Arrays.equals(segments, other.segments))
 91  0
                         return false;
 92  2
                 return true;
 93  
         }
 94  
 
 95  
         /**
 96  
          * Returns a copy of this tree path with one segment removed from the end, or <code>null</code> if this tree path
 97  
          * has no segments.
 98  
          * 
 99  
          * @return a tree path
 100  
          */
 101  
         public TreePath getParentPath() {
 102  4
                 int segmentCount = getSegmentCount();
 103  4
                 if (segmentCount < 1) {
 104  1
                         return null;
 105  3
                 } else if (segmentCount == 1) {
 106  1
                         return EMPTY;
 107  
                 }
 108  2
                 Object[] parentSegments = new Object[segmentCount - 1];
 109  2
                 System.arraycopy(segments, 0, parentSegments, 0, segmentCount - 1);
 110  2
                 return new TreePath(parentSegments);
 111  
         }
 112  
 
 113  
         /**
 114  
          * Removes the last path component from the tree and returns it.
 115  
          * 
 116  
          * @return the last path component after removing it from the tree.
 117  
          */
 118  
         public Object pop() {
 119  2
                 int segmentCount = getSegmentCount();
 120  2
                 if (segmentCount == 0) {
 121  1
                         return null;
 122  
                 }
 123  1
                 Object lastSegment = getLastSegment();
 124  
 
 125  1
                 Object[] parentSegments = new Object[segmentCount - 1];
 126  1
                 System.arraycopy(segments, 0, parentSegments, 0, segmentCount - 1);
 127  1
                 segments = parentSegments;
 128  1
                 return lastSegment;
 129  
         }
 130  
 }