PeerInfoProvider.java revision d153b6000977e5e36448e4faf1a5c602c461057f
1/*
2 * Copyright (C) 2017 The Android Open Source Project
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 */
16package org.conscrypt;
17
18/**
19 * A provider for the peer host and port information.
20 */
21abstract class PeerInfoProvider {
22    private static final PeerInfoProvider NULL_PEER_INFO_PROVIDER = new PeerInfoProvider() {
23        @Override
24        public String getHostnameOrIP() {
25            return null;
26        }
27
28        @Override
29        public int getPort() {
30            return -1;
31        }
32    };
33
34    /**
35     * This method attempts to create a textual representation of the peer host or IP. Does
36     * not perform a reverse DNS lookup. This is typically used during session creation.
37     */
38    abstract String getHostnameOrIP();
39
40    /**
41     * Gets the port of the peer.
42     */
43    abstract int getPort();
44
45    static PeerInfoProvider nullProvider() {
46        return NULL_PEER_INFO_PROVIDER;
47    }
48
49    static PeerInfoProvider forHostAndPort(final String host, final int port) {
50        return new PeerInfoProvider() {
51            @Override
52            public String getHostnameOrIP() {
53                return host;
54            }
55
56            @Override
57            public int getPort() {
58                return port;
59            }
60        };
61    }
62}
63