SAMZA-1249: Fix equality for WindowKey for Non-keyed tumbling windows
[samza.git] / samza-api / src / main / java / org / apache / samza / operators / windows / WindowKey.java
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.samza.operators.windows;
20
21 /**
22 * Key for a {@link WindowPane} emitted from a {@link Window}.
23 *
24 * @param <K> the type of the key in the incoming message.
25 * Windows that are not keyed have a {@link Void} key type.
26 *
27 */
28 public class WindowKey<K> {
29 /**
30 * A (key,paneId) tuple uniquely identifies an emission from a window. For instance, in case of keyed-tumbling time windows,
31 * the key is provided by the keyExtractor function, and the paneId is the start of the time window boundary. In case
32 * of session windows, the key is provided by the keyExtractor function, and the paneId is the time at which the earliest
33 * message in the window arrived.
34 */
35 private final K key;
36
37 private final String paneId;
38
39 public WindowKey(K key, String paneId) {
40 this.key = key;
41 this.paneId = paneId;
42 }
43
44 public K getKey() {
45 return key;
46 }
47
48 public String getPaneId() {
49 return paneId;
50 }
51
52 @Override
53 public String toString() {
54 String wndKey = "";
55 if (!(key instanceof Void)) {
56 wndKey = String.format("%s:", key.toString());
57 }
58 return String.format("%s%s", wndKey, paneId);
59 }
60
61 @Override
62 public boolean equals(Object o) {
63 if (this == o) return true;
64 if (o == null || getClass() != o.getClass()) return false;
65
66 WindowKey<?> windowKey = (WindowKey<?>) o;
67
68 if (key != null ? !key.equals(windowKey.key) : windowKey.key != null) return false;
69 return !(paneId != null ? !paneId.equals(windowKey.paneId) : windowKey.paneId != null);
70
71 }
72
73 @Override
74 public int hashCode() {
75 int result = key != null ? key.hashCode() : 0;
76 result = 31 * result + (paneId != null ? paneId.hashCode() : 0);
77 return result;
78 }
79 }