Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8fe503f1 authored by Chalard Jean's avatar Chalard Jean
Browse files

[MS05] Pretty print the data classes of IPMS.

Test: A commando of monkeys has been dispatched to make sure
      this pretty-printing is very, very pretty.
      Also one human has made sure of this by printing instances
      to the log.
Bug: 116512211

Change-Id: I7df77a997a982a0b3d444502cc69de5544f70736
parent 0150bd92
Loading
Loading
Loading
Loading
+49 −0
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.StringJoiner;

/**
 * A POD object to represent attributes of a single L2 network entry.
@@ -207,4 +208,52 @@ public class NetworkAttributes {
    public int hashCode() {
        return Objects.hash(assignedV4Address, groupHint, dnsAddresses, mtu);
    }

    /** Pretty print */
    @Override
    public String toString() {
        final StringJoiner resultJoiner = new StringJoiner(" ", "{", "}");
        final ArrayList<String> nullFields = new ArrayList<>();

        if (null != assignedV4Address) {
            resultJoiner.add("assignedV4Addr :");
            resultJoiner.add(assignedV4Address.toString());
        } else {
            nullFields.add("assignedV4Addr");
        }

        if (null != groupHint) {
            resultJoiner.add("groupHint :");
            resultJoiner.add(groupHint);
        } else {
            nullFields.add("groupHint");
        }

        if (null != dnsAddresses) {
            resultJoiner.add("dnsAddr : [");
            for (final InetAddress addr : dnsAddresses) {
                resultJoiner.add(addr.getHostAddress());
            }
            resultJoiner.add("]");
        } else {
            nullFields.add("dnsAddr");
        }

        if (null != mtu) {
            resultJoiner.add("mtu :");
            resultJoiner.add(mtu.toString());
        } else {
            nullFields.add("mtu");
        }

        if (!nullFields.isEmpty()) {
            resultJoiner.add("; Null fields : [");
            for (final String field : nullFields) {
                resultJoiner.add(field);
            }
            resultJoiner.add("]");
        }

        return resultJoiner.toString();
    }
}
+15 −0
Original line number Diff line number Diff line
@@ -128,4 +128,19 @@ public class SameL3NetworkResponse {
    public int hashCode() {
        return Objects.hash(l2Key1, l2Key2, confidence);
    }

    @Override
    /** Pretty print */
    public String toString() {
        switch (getNetworkSameness()) {
            case NETWORK_SAME:
                return "\"" + l2Key1 + "\" same L3 network as \"" + l2Key2 + "\"";
            case NETWORK_DIFFERENT:
                return "\"" + l2Key1 + "\" different L3 network from \"" + l2Key2 + "\"";
            case NETWORK_NEVER_CONNECTED:
                return "\"" + l2Key1 + "\" can't be tested against \"" + l2Key2 + "\"";
            default:
                return "Buggy sameness value ? \"" + l2Key1 + "\", \"" + l2Key2 + "\"";
        }
    }
}
+10 −0
Original line number Diff line number Diff line
@@ -49,4 +49,14 @@ public class Status {
    public boolean isSuccess() {
        return SUCCESS == resultCode;
    }

    /** Pretty print */
    @Override
    public String toString() {
        switch (resultCode) {
            case SUCCESS: return "SUCCESS";
            case ERROR_DATABASE_CANNOT_BE_OPENED: return "DATABASE CANNOT BE OPENED";
            default: return "Unknown value ?!";
        }
    }
}
+44 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.net.ipmemorystore;

import android.annotation.NonNull;

/** {@hide} */
public class Utils {
    /** Pretty print */
    public static String blobToString(final Blob blob) {
        final StringBuilder sb = new StringBuilder("Blob : [");
        if (blob.data.length <= 24) {
            appendByteArray(sb, blob.data, 0, blob.data.length);
        } else {
            appendByteArray(sb, blob.data, 0, 16);
            sb.append("...");
            appendByteArray(sb, blob.data, blob.data.length - 8, blob.data.length);
        }
        sb.append("]");
        return sb.toString();
    }

    // Adds the hex representation of the array between the specified indices (inclusive, exclusive)
    private static void appendByteArray(@NonNull final StringBuilder sb, @NonNull final byte[] ar,
            final int from, final int to) {
        for (int i = from; i < to; ++i) {
            sb.append(String.format("%02X", ar[i]));
        }
    }
}