1 package com.github.triceo.splitlog; 2 3 import com.github.triceo.splitlog.api.Message; 4 import com.github.triceo.splitlog.api.MessageConsumer; 5 import com.github.triceo.splitlog.api.MessageDeliveryStatus; 6 import com.github.triceo.splitlog.api.MessageListener; 7 import com.github.triceo.splitlog.api.MessageProducer; 8 9 final class DefaultMessageConsumer<P extends MessageProducer<P>> implements MessageConsumer<P> { 10 11 private final MessageListener<P> listener; 12 private final P producer; 13 14 public DefaultMessageConsumer(final MessageListener<P> listener, final P producer) { 15 if ((producer == null) || (listener == null)) { 16 throw new IllegalArgumentException("Neither producer nor listener may be null."); 17 } 18 this.producer = producer; 19 this.listener = listener; 20 } 21 22 @SuppressWarnings("unchecked") 23 @Override 24 public boolean equals(final Object obj) { 25 if (this == obj) { 26 return true; 27 } 28 if (obj == null) { 29 return false; 30 } 31 if (this.getClass() != obj.getClass()) { 32 return false; 33 } 34 final DefaultMessageConsumer<P> other = (DefaultMessageConsumer<P>) obj; 35 if (this.listener == null) { 36 if (other.listener != null) { 37 return false; 38 } 39 } else if (!this.listener.equals(other.listener)) { 40 return false; 41 } 42 if (this.producer == null) { 43 if (other.producer != null) { 44 return false; 45 } 46 } else if (!this.producer.equals(other.producer)) { 47 return false; 48 } 49 return true; 50 } 51 52 @Override 53 public int hashCode() { 54 final int prime = 31; 55 int result = 1; 56 result = (prime * result) + ((this.listener == null) ? 0 : this.listener.hashCode()); 57 result = (prime * result) + ((this.producer == null) ? 0 : this.producer.hashCode()); 58 return result; 59 } 60 61 @Override 62 public boolean isStopped() { 63 return !this.producer.isConsuming(this); 64 } 65 66 @Override 67 public void messageReceived(final Message message, final MessageDeliveryStatus status, final P producer) { 68 this.listener.messageReceived(message, status, producer); 69 } 70 71 @Override 72 public boolean stop() { 73 return this.producer.stopConsuming(this); 74 } 75 76 @Override 77 public String toString() { 78 final StringBuilder builder = new StringBuilder(); 79 builder.append("DefaultMessageConsumer ["); 80 if (this.producer != null) { 81 builder.append("producer=").append(this.producer).append(", "); 82 } 83 if (this.listener != null) { 84 builder.append("listener=").append(this.listener); 85 } 86 builder.append("]"); 87 return builder.toString(); 88 } 89 90 }