| 1 | /* |
|---|
| 2 | * Copyright 2009 NCHOVY |
|---|
| 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 | package org.krakenapps.filter; |
|---|
| 17 | |
|---|
| 18 | import java.util.HashMap; |
|---|
| 19 | import java.util.Map; |
|---|
| 20 | |
|---|
| 21 | import org.krakenapps.filter.impl.DefaultMessage; |
|---|
| 22 | |
|---|
| 23 | /** |
|---|
| 24 | * Builds a message of specific message specification. This class is for |
|---|
| 25 | * convenience. |
|---|
| 26 | * |
|---|
| 27 | * @author xeraph |
|---|
| 28 | * @since 1.0.0 |
|---|
| 29 | */ |
|---|
| 30 | public class MessageBuilder { |
|---|
| 31 | private MessageSpec spec; |
|---|
| 32 | private Map<String, Object> fields; |
|---|
| 33 | private Map<String, Object> headers; |
|---|
| 34 | |
|---|
| 35 | /** |
|---|
| 36 | * Prepare a builder of specific message specification. |
|---|
| 37 | * |
|---|
| 38 | * @param spec |
|---|
| 39 | * the message specification |
|---|
| 40 | */ |
|---|
| 41 | public MessageBuilder(MessageSpec spec) { |
|---|
| 42 | this.spec = spec; |
|---|
| 43 | this.fields = new HashMap<String, Object>(); |
|---|
| 44 | this.headers = new HashMap<String, Object>(); |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | /** |
|---|
| 48 | * Copy from the other message |
|---|
| 49 | * |
|---|
| 50 | * @param message |
|---|
| 51 | * the source message |
|---|
| 52 | * @return builder for method chaining |
|---|
| 53 | */ |
|---|
| 54 | public MessageBuilder setBase(Message message) { |
|---|
| 55 | this.spec = message.getMessageSpec(); |
|---|
| 56 | |
|---|
| 57 | for (String key : message.keySet()) { |
|---|
| 58 | fields.put(key, message.get(key)); |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | for (String key : message.headerKeySet()) { |
|---|
| 62 | headers.put(key, message.getHeader(key)); |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | return this; |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | /** |
|---|
| 69 | * Sets a property |
|---|
| 70 | * |
|---|
| 71 | * @param key |
|---|
| 72 | * the name of the property |
|---|
| 73 | * @param value |
|---|
| 74 | * the value of the property |
|---|
| 75 | * @return builder for method chaining |
|---|
| 76 | */ |
|---|
| 77 | public MessageBuilder set(String key, Object value) { |
|---|
| 78 | fields.put(key, value); |
|---|
| 79 | return this; |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | /** |
|---|
| 83 | * Sets a header |
|---|
| 84 | * |
|---|
| 85 | * @param key |
|---|
| 86 | * the name of the header |
|---|
| 87 | * @param value |
|---|
| 88 | * the value of the header |
|---|
| 89 | * @return builder for method chaining |
|---|
| 90 | */ |
|---|
| 91 | public MessageBuilder setHeader(String key, Object value) { |
|---|
| 92 | headers.put(key, value); |
|---|
| 93 | return this; |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | /** |
|---|
| 97 | * Creates a message instance. |
|---|
| 98 | * |
|---|
| 99 | * @return the immutable message instance. |
|---|
| 100 | */ |
|---|
| 101 | public Message build() { |
|---|
| 102 | Message message = new DefaultMessage(spec, fields, headers); |
|---|
| 103 | fields = new HashMap<String, Object>(); |
|---|
| 104 | headers = new HashMap<String, Object>(); |
|---|
| 105 | return message; |
|---|
| 106 | } |
|---|
| 107 | } |
|---|