1 /* 2 * Copyright 2002-2018 the original author or authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 module hunt.stomp.handler.AbstractMessageCondition; 18 19 import hunt.stomp.handler.MessageCondition; 20 21 import hunt.collection; 22 import hunt.text.StringBuilder; 23 24 25 26 /** 27 * Base class for {@code MessageCondition's} that pre-declares abstract methods 28 * {@link #getContent()} and {@link #getToStringInfix()} in order to provide 29 * implementations of {@link #equals(Object)}, {@link #toHash()}, and 30 * {@link #toString()}. 31 * 32 * @author Rossen Stoyanchev 33 * @since 4.0 34 * @param (T) the kind of condition that this condition can be combined with or compared to 35 */ 36 abstract class AbstractMessageCondition(T, U) : MessageCondition!(T) { 37 38 override 39 bool opEquals(Object other) { 40 if (other is null) 41 return false; 42 43 if (this is other) 44 return true; 45 46 auto ot = cast(typeof(this)) other; 47 if(ot is null) 48 return false; 49 return getContent() == ot.getContent(); 50 } 51 52 override 53 size_t toHash() @trusted nothrow { 54 size_t h = 0; 55 try { 56 h = getContent().toHash(); 57 } catch(Exception e) { 58 59 } 60 return h; 61 } 62 63 override 64 string toString() { 65 StringBuilder builder = new StringBuilder("["); 66 // for (Iterator<?> iterator = getContent().iterator(); iterator.hasNext();) { 67 // Object expression = iterator.next(); 68 // builder.append(expression.toString()); 69 // if (iterator.hasNext()) { 70 // builder.append(getToStringInfix()); 71 // } 72 // } 73 builder.append("]"); 74 return builder.toString(); 75 } 76 77 78 /** 79 * Return the collection of objects the message condition is composed of 80 * (e.g. destination patterns), never {@code null}. 81 */ 82 protected abstract Collection!U getContent(); 83 84 /** 85 * The notation to use when printing discrete items of content. 86 * For example " || " for URL patterns or " && " for param expressions. 87 */ 88 protected abstract string getToStringInfix(); 89 90 }