| 1 | /******************************************************************************* | 
| 2 |  * Copyright (c) 2008, 2009 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.pde.api.tools.internal.problems; | 
| 12 |   | 
| 13 | import org.eclipse.core.runtime.Assert; | 
| 14 | import org.eclipse.pde.api.tools.internal.provisional.problems.IApiProblem; | 
| 15 | import org.eclipse.pde.api.tools.internal.provisional.problems.IApiProblemFilter; | 
| 16 |   | 
| 17 | /** | 
| 18 |  * Base implementation of {@link IApiProblemFilter} | 
| 19 |  *  | 
| 20 |  * @since 1.0.0 | 
| 21 |  */ | 
| 22 | public class ApiProblemFilter implements IApiProblemFilter, Cloneable { | 
| 23 |   | 
| 24 |         public static final String HANDLE_ARGUMENTS_DELIMITER = ","; //$NON-NLS-1$ | 
| 25 |         public static final String HANDLE_DELIMITER = "%]"; //$NON-NLS-1$ | 
| 26 |   | 
| 27 |         private String fComponentId = null; | 
| 28 |         private IApiProblem fProblem = null; | 
| 29 |          | 
| 30 |         /** | 
| 31 |          * Constructor | 
| 32 |          *  | 
| 33 |          * @param componentid | 
| 34 |          * @param problem | 
| 35 |          */ | 
| 36 |         public ApiProblemFilter(String componentid, IApiProblem problem) { | 
| 37 |                 fComponentId = componentid; | 
| 38 |                 Assert.isNotNull(problem); | 
| 39 |                 fProblem = problem; | 
| 40 |         } | 
| 41 |          | 
| 42 |         /* (non-Javadoc) | 
| 43 |          * @see org.eclipse.pde.api.tools.IApiProblemFilter#getComponentId() | 
| 44 |          */ | 
| 45 |         public String getComponentId() { | 
| 46 |                 return fComponentId; | 
| 47 |         } | 
| 48 |          | 
| 49 |         /* (non-Javadoc) | 
| 50 |          * @see java.lang.Object#equals(java.lang.Object) | 
| 51 |          */ | 
| 52 |         public boolean equals(Object obj) { | 
| 53 |                 if(obj instanceof IApiProblemFilter) { | 
| 54 |                         IApiProblemFilter filter = (IApiProblemFilter) obj; | 
| 55 |                         return elementsEqual(filter.getComponentId(), fComponentId) && | 
| 56 |                                         filter.getUnderlyingProblem().equals(fProblem); | 
| 57 |                 } | 
| 58 |                 else if(obj instanceof IApiProblem) { | 
| 59 |                         return fProblem.equals(obj); | 
| 60 |                 } | 
| 61 |                 return super.equals(obj); | 
| 62 |         } | 
| 63 |          | 
| 64 |         /* (non-Javadoc) | 
| 65 |          * @see java.lang.Object#hashCode() | 
| 66 |          */ | 
| 67 |         public int hashCode() { | 
| 68 |                 return fProblem.hashCode() + fComponentId.hashCode(); | 
| 69 |         } | 
| 70 |          | 
| 71 |         /** | 
| 72 |          * Returns if the two specified objects are equal. | 
| 73 |          * Objects are considered equal if: | 
| 74 |          * <ol> | 
| 75 |          * <li>they are both null</li> | 
| 76 |          * <li>they are equal via the default .equals() method</li> | 
| 77 |          * </ol> | 
| 78 |          * @param s1 | 
| 79 |          * @param s2 | 
| 80 |          * @return true if the objects are equal, false otherwise | 
| 81 |          */ | 
| 82 |         private boolean elementsEqual(Object s1, Object s2) { | 
| 83 |                 return (s1 == null && s2 == null) || (s1 != null && s1.equals(s2)); | 
| 84 |         } | 
| 85 |          | 
| 86 |         /* (non-Javadoc) | 
| 87 |          * @see java.lang.Object#toString() | 
| 88 |          */ | 
| 89 |         public String toString() { | 
| 90 |                 StringBuffer buffer = new StringBuffer(); | 
| 91 |                 buffer.append("Filter for : "); //$NON-NLS-1$ | 
| 92 |                 buffer.append(fProblem.toString()); | 
| 93 |                 return buffer.toString(); | 
| 94 |         } | 
| 95 |          | 
| 96 |         /* (non-Javadoc) | 
| 97 |          * @see java.lang.Object#clone() | 
| 98 |          */ | 
| 99 |         public Object clone() { | 
| 100 |                 return new ApiProblemFilter(this.fComponentId, fProblem); | 
| 101 |         } | 
| 102 |   | 
| 103 |         /* (non-Javadoc) | 
| 104 |          * @see org.eclipse.pde.api.tools.internal.provisional.IApiProblemFilter#getUnderlyingProblem() | 
| 105 |          */ | 
| 106 |         public IApiProblem getUnderlyingProblem() { | 
| 107 |                 return fProblem; | 
| 108 |         } | 
| 109 |          | 
| 110 |         /** | 
| 111 |          * @return returns a handle that can be used to identify the filter | 
| 112 |          */ | 
| 113 |         public String getHandle() { | 
| 114 |                 StringBuffer buffer = new StringBuffer(); | 
| 115 |                 buffer.append(fProblem.getId()); | 
| 116 |                 buffer.append(HANDLE_DELIMITER); | 
| 117 |                 buffer.append(fProblem.getResourcePath()); | 
| 118 |                 buffer.append(HANDLE_DELIMITER); | 
| 119 |                 buffer.append(fProblem.getTypeName()); | 
| 120 |                 buffer.append(HANDLE_DELIMITER); | 
| 121 |                 String[] margs = fProblem.getMessageArguments(); | 
| 122 |                 for(int i = 0; i < margs.length; i++) { | 
| 123 |                         buffer.append(margs[i]); | 
| 124 |                         if(i < margs.length-1) { | 
| 125 |                                 buffer.append(HANDLE_ARGUMENTS_DELIMITER); | 
| 126 |                         } | 
| 127 |                 } | 
| 128 |                 return buffer.toString(); | 
| 129 |         } | 
| 130 | } |