001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.tools.template_engine; 003 004import org.openstreetmap.josm.data.osm.search.SearchCompiler.Match; 005 006/** 007 * Conditional {@link TemplateEntry} that executes another template in case a search expression applies 008 * to the given data provider. 009 */ 010public class SearchExpressionCondition implements TemplateEntry { 011 012 private final Match condition; 013 private final TemplateEntry text; 014 015 /** 016 * Creates a new {@link SearchExpressionCondition}. 017 * @param condition the match condition that is checked before applying the child template 018 * @param text the child template to execute in case the condition is fulfilled 019 */ 020 public SearchExpressionCondition(Match condition, TemplateEntry text) { 021 this.condition = condition; 022 this.text = text; 023 } 024 025 @Override 026 public void appendText(StringBuilder result, TemplateEngineDataProvider dataProvider) { 027 text.appendText(result, dataProvider); 028 } 029 030 @Override 031 public boolean isValid(TemplateEngineDataProvider dataProvider) { 032 return dataProvider.evaluateCondition(condition); 033 } 034 035 @Override 036 public String toString() { 037 return condition + " '" + text + '\''; 038 } 039 040 @Override 041 public int hashCode() { 042 final int prime = 31; 043 int result = 1; 044 result = prime * result + ((condition == null) ? 0 : condition.hashCode()); 045 result = prime * result + ((text == null) ? 0 : text.hashCode()); 046 return result; 047 } 048 049 @Override 050 public boolean equals(Object obj) { 051 if (this == obj) 052 return true; 053 if (obj == null || getClass() != obj.getClass()) 054 return false; 055 SearchExpressionCondition other = (SearchExpressionCondition) obj; 056 if (condition == null) { 057 if (other.condition != null) 058 return false; 059 } else if (!condition.equals(other.condition)) 060 return false; 061 if (text == null) { 062 if (other.text != null) 063 return false; 064 } else if (!text.equals(other.text)) 065 return false; 066 return true; 067 } 068}