1 /* 2 * Copyright (C) 2022 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 */ 16 17 package com.android.adservices.service.measurement; 18 19 import static com.android.adservices.service.measurement.XNetworkData.XNetworkDataContract.KEY_OFFSET; 20 21 import android.annotation.NonNull; 22 import android.annotation.Nullable; 23 24 import com.android.adservices.LogUtil; 25 import com.android.adservices.service.measurement.util.UnsignedLong; 26 27 import org.json.JSONException; 28 import org.json.JSONObject; 29 30 import java.util.Objects; 31 import java.util.Optional; 32 33 /** POJO for XNetworkData. */ 34 public class XNetworkData { 35 36 private final Optional<UnsignedLong> mKeyOffset; 37 XNetworkData(@onNull XNetworkData.Builder builder)38 private XNetworkData(@NonNull XNetworkData.Builder builder) { 39 mKeyOffset = builder.mKeyOffset; 40 } 41 42 @Override equals(Object obj)43 public boolean equals(Object obj) { 44 if (!(obj instanceof XNetworkData)) { 45 return false; 46 } 47 XNetworkData xNetworkData = (XNetworkData) obj; 48 return Objects.equals(mKeyOffset, xNetworkData.mKeyOffset); 49 } 50 51 @Override hashCode()52 public int hashCode() { 53 return Objects.hash(mKeyOffset); 54 } 55 56 /** Returns the value of keyOffset as Long */ getKeyOffset()57 public Optional<UnsignedLong> getKeyOffset() { 58 return mKeyOffset; 59 } 60 61 /** Builder for {@link XNetworkData}. */ 62 public static final class Builder { 63 private Optional<UnsignedLong> mKeyOffset; 64 Builder()65 public Builder() { 66 mKeyOffset = Optional.empty(); 67 } 68 Builder(@onNull JSONObject jsonObject)69 public Builder(@NonNull JSONObject jsonObject) throws JSONException { 70 if (!jsonObject.isNull(KEY_OFFSET)) { 71 String keyOffset = jsonObject.getString(KEY_OFFSET); 72 // Unassigned in order to validate the long value 73 try { 74 mKeyOffset = Optional.of(new UnsignedLong(keyOffset)); 75 } catch (NumberFormatException e) { 76 LogUtil.d(e, "XNetworkData.Builder: Failed to parse keyOffset."); 77 // Wrapped into JSONException so that it does not crash and becomes a checked 78 // Exception that is caught by the caller. 79 throw new JSONException("Failed to parse keyOffset", e); 80 } 81 } 82 } 83 84 /** See {@link XNetworkData#getKeyOffset()}. */ 85 @NonNull setKeyOffset(@ullable UnsignedLong keyOffset)86 public Builder setKeyOffset(@Nullable UnsignedLong keyOffset) { 87 mKeyOffset = Optional.ofNullable(keyOffset); 88 return this; 89 } 90 91 /** Build the {@link XNetworkData}. */ 92 @NonNull build()93 public XNetworkData build() { 94 return new XNetworkData(this); 95 } 96 } 97 98 /** Constants related to XNetworkData. */ 99 public interface XNetworkDataContract { 100 String KEY_OFFSET = "key_offset"; 101 } 102 } 103