]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.server/src/org/simantics/db/server/internal/ServerAddress.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.db.server / src / org / simantics / db / server / internal / ServerAddress.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
3  * Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementatio
11  *******************************************************************************/
12 package org.simantics.db.server.internal;
13
14 import java.net.InetSocketAddress;
15
16 /**
17  * @author J-P Laine
18  */
19 public class ServerAddress {
20     private InetSocketAddress socketAddress;
21     private String dbid;
22
23     public ServerAddress(String hostAndPort, String dbid) {
24         this(hostAndPort);
25         this.dbid = dbid;
26     }
27
28     public ServerAddress(String hostAndPort) {
29         assert (hostAndPort != null);
30
31         String[] split = hostAndPort.split(":");
32         if (split.length != 2)
33             throw new IllegalArgumentException("address does not contain a port, missing ':' character");
34         this.socketAddress = InetSocketAddress.createUnresolved(split[0], Integer.parseInt(split[1]));
35         this.dbid = null;
36     }
37
38     public ServerAddress(String host, int port) {
39         assert (host != null);
40         this.socketAddress = new InetSocketAddress(host, port);
41         this.dbid = null;
42     }
43
44     public ServerAddress(String host, int port, String dbid) {
45         assert (host != null);
46         this.socketAddress = InetSocketAddress.createUnresolved(host, port);
47         this.dbid = dbid;
48     }
49
50     public ServerAddress(InetSocketAddress socketAddress) {
51         assert (socketAddress != null);
52         this.socketAddress = socketAddress;
53         this.dbid = null;
54     }
55
56     public ServerAddress(InetSocketAddress socketAddress, String dbid) {
57         assert (socketAddress != null);
58         this.socketAddress = socketAddress;
59         this.dbid = dbid;
60     }
61
62     public InetSocketAddress getAddress() {
63         return socketAddress;
64     }
65
66     public String getDbid() {
67         return dbid;
68     }
69
70     @Override
71     public int hashCode() {
72         return socketAddress.hashCode();
73     }
74
75     @Override
76     public boolean equals(Object other) {
77         if (this == other) return true;
78         if (other == null || !(getClass().equals(other.getClass()))) return false;
79         ServerAddress r = (ServerAddress) other;
80         return r.socketAddress.equals(socketAddress) && (r.dbid != null && r.dbid.equals(dbid) || r.dbid == null && dbid == null);
81     }
82
83     @Override
84     public String toString() {
85         if(dbid != null) {
86             return socketAddress.toString()+" / "+dbid;
87         } else {
88             return socketAddress.toString();
89         }
90     }
91 }