Coverage Report - org.argouml.util.PredicateStringMatch
 
Classes in this File Line Coverage Branch Coverage Complexity
PredicateStringMatch
0%
0/37
0%
0/24
7.333
 
 1  
 /* $Id: PredicateStringMatch.java 17887 2010-01-12 21:17:18Z linus $
 2  
  *****************************************************************************
 3  
  * Copyright (c) 2009 Contributors - see below
 4  
  * All rights reserved. This program and the accompanying materials
 5  
  * are made available under the terms of the Eclipse Public License v1.0
 6  
  * which accompanies this distribution, and is available at
 7  
  * http://www.eclipse.org/legal/epl-v10.html
 8  
  *
 9  
  * Contributors:
 10  
  *    tfmorris
 11  
  *****************************************************************************
 12  
  *
 13  
  * Some portions of this file was previously release using the BSD License:
 14  
  */
 15  
 
 16  
 // Copyright (c) 1996-2008 The Regents of the University of California. All
 17  
 // Rights Reserved. Permission to use, copy, modify, and distribute this
 18  
 // software and its documentation without fee, and without a written
 19  
 // agreement is hereby granted, provided that the above copyright notice
 20  
 // and this paragraph appear in all copies. This software program and
 21  
 // documentation are copyrighted by The Regents of the University of
 22  
 // California. The software program and documentation are supplied "AS
 23  
 // IS", without any accompanying services from The Regents. The Regents
 24  
 // does not warrant that the operation of the program will be
 25  
 // uninterrupted or error-free. The end-user understands that the program
 26  
 // was developed for research purposes and is advised not to rely
 27  
 // exclusively on the program for any reason. IN NO EVENT SHALL THE
 28  
 // UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
 29  
 // SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
 30  
 // ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
 31  
 // THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
 32  
 // SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY
 33  
 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 34  
 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
 35  
 // PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
 36  
 // CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT,
 37  
 // UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 38  
 
 39  
 package org.argouml.util;
 40  
 
 41  
 import java.util.StringTokenizer;
 42  
 
 43  
 /**
 44  
  * Predicate to match strings/substrings.  It only supports simple
 45  
  * wildcard processing, not full regex style matching.
 46  
  */
 47  
 public class PredicateStringMatch implements Predicate {
 48  
 
 49  0
     public static int MAX_PATS = 10;
 50  
 
 51  
     private String patterns[];
 52  
 
 53  
     private int patternCount;
 54  
 
 55  0
     protected PredicateStringMatch(String matchPatterns[], int count) {
 56  0
         patterns = matchPatterns;
 57  0
         patternCount = count;
 58  0
     }
 59  
 
 60  
     public static Predicate create(String pattern) {
 61  0
         pattern = pattern.trim();
 62  0
         if ("*".equals(pattern) || "".equals(pattern)) {
 63  0
             return PredicateTrue.getInstance();
 64  
         }
 65  0
         String pats[] = new String[MAX_PATS];
 66  0
         int count = 0;
 67  0
         if (pattern.startsWith("*")) {
 68  0
             pats[count++] = "";
 69  
         }
 70  0
         StringTokenizer st = new StringTokenizer(pattern, "*");
 71  0
         while (st.hasMoreElements()) {
 72  0
             String token = st.nextToken();
 73  0
             pats[count++] = token;
 74  0
         }
 75  0
         if (pattern.endsWith("*")) {
 76  0
             pats[count++] = "";
 77  
         }
 78  0
         if (count == 0) {
 79  0
             return PredicateTrue.getInstance();
 80  
         }
 81  0
         if (count == 1) {
 82  0
             return new PredicateEquals(pats[0]);
 83  
         }
 84  0
         return new PredicateStringMatch(pats, count);
 85  
     }
 86  
 
 87  
     public boolean evaluate(Object o) {
 88  0
         if (o == null) {
 89  0
             return false;
 90  
         }
 91  0
         String target = o.toString();
 92  0
         if (!target.startsWith(patterns[0])) {
 93  0
             return false;
 94  
         }
 95  0
         if (!target.endsWith(patterns[patternCount - 1])) {
 96  0
             return false;
 97  
         }
 98  0
         for (String pattern : patterns) {
 99  0
             int index = (target + "*").indexOf(pattern);
 100  0
             if (index == -1) {
 101  0
                 return false;
 102  
             }
 103  0
             target = target.substring(index + pattern.length());
 104  
         }
 105  0
         return true;
 106  
     }
 107  
 
 108  
 }