WIP:exception
[demos/kafka/chat] / src / main / java / de / juplo / kafka / chat / backend / domain / ShardNotOwnedException.java
1 package de.juplo.kafka.chat.backend.domain;
2
3 import lombok.Getter;
4
5 import java.util.Arrays;
6 import java.util.Collection;
7 import java.util.Iterator;
8 import java.util.stream.Collectors;
9
10
11 public class ShardNotOwnedException extends IllegalStateException
12 {
13   @Getter
14   private final ChatHomeService chatHomeService;
15   @Getter
16   private final ChatRoomInfo chatRoomInfo;
17   @Getter
18   private final int shard;
19   @Getter
20   private final int[] ownedShards;
21
22
23   public ShardNotOwnedException(
24       ChatHomeService chatHomeService,
25       ChatRoomInfo chatRoomInfo,
26       int shard,
27       Collection<Integer> ownedShards)
28   {
29     this(
30         chatHomeService,
31         chatRoomInfo,
32         shard,
33         ShardNotOwnedException.toArray(ownedShards));
34   }
35
36   public ShardNotOwnedException(
37       ChatHomeService chatHomeService,
38       ChatRoomInfo chatRoomInfo,
39       int shard,
40       int[] ownedShards)
41   {
42     super(
43         chatHomeService +
44         " does not own the shard " +
45         shard +
46         " for ChatRoom " +
47         chatRoomInfo +
48         " owned shards: " +
49         Arrays
50             .stream(ownedShards)
51             .mapToObj(ownedShard -> Integer.toString(ownedShard))
52             .collect(Collectors.joining(", ")));
53     this.chatHomeService = chatHomeService;
54     this.chatRoomInfo = chatRoomInfo;
55     this.shard = shard;
56     this.ownedShards = ownedShards;
57   }
58
59
60   private static int[] toArray(Collection<Integer> collection)
61   {
62     int[] array = new int[collection.size()];
63     Iterator<Integer> iterator = collection.iterator();
64     for (int i = 0; iterator.hasNext(); i++)
65       array[i] = iterator.next();
66     return array;
67   }
68 }