Home | History | Annotate | Download | only in internal
      1 /*
      2  * Copyright 2018, OpenCensus Authors
      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 io.opencensus.trace.internal;
     18 
     19 import io.opencensus.common.Internal;
     20 import io.opencensus.internal.Utils;
     21 
     22 /**
     23  * Helper class to convert/cast between for {@link io.opencensus.trace.MessageEvent} and {@link
     24  * io.opencensus.trace.NetworkEvent}.
     25  */
     26 @Internal
     27 @SuppressWarnings("deprecation")
     28 public final class BaseMessageEventUtils {
     29   /**
     30    * Cast or convert a {@link io.opencensus.trace.BaseMessageEvent} to {@link
     31    * io.opencensus.trace.MessageEvent}.
     32    *
     33    * <p>Warning: if the input is a {@code io.opencensus.trace.NetworkEvent} and contains {@code
     34    * kernelTimestamp} information, this information will be dropped.
     35    *
     36    * @param event the {@code BaseMessageEvent} that is being cast or converted.
     37    * @return a {@code MessageEvent} representation of the input.
     38    */
     39   public static io.opencensus.trace.MessageEvent asMessageEvent(
     40       io.opencensus.trace.BaseMessageEvent event) {
     41     Utils.checkNotNull(event, "event");
     42     if (event instanceof io.opencensus.trace.MessageEvent) {
     43       return (io.opencensus.trace.MessageEvent) event;
     44     }
     45     io.opencensus.trace.NetworkEvent networkEvent = (io.opencensus.trace.NetworkEvent) event;
     46     io.opencensus.trace.MessageEvent.Type type =
     47         (networkEvent.getType() == io.opencensus.trace.NetworkEvent.Type.RECV)
     48             ? io.opencensus.trace.MessageEvent.Type.RECEIVED
     49             : io.opencensus.trace.MessageEvent.Type.SENT;
     50     return io.opencensus.trace.MessageEvent.builder(type, networkEvent.getMessageId())
     51         .setUncompressedMessageSize(networkEvent.getUncompressedMessageSize())
     52         .setCompressedMessageSize(networkEvent.getCompressedMessageSize())
     53         .build();
     54   }
     55 
     56   /**
     57    * Cast or convert a {@link io.opencensus.trace.BaseMessageEvent} to {@link
     58    * io.opencensus.trace.NetworkEvent}.
     59    *
     60    * @param event the {@code BaseMessageEvent} that is being cast or converted.
     61    * @return a {@code io.opencensus.trace.NetworkEvent} representation of the input.
     62    */
     63   public static io.opencensus.trace.NetworkEvent asNetworkEvent(
     64       io.opencensus.trace.BaseMessageEvent event) {
     65     Utils.checkNotNull(event, "event");
     66     if (event instanceof io.opencensus.trace.NetworkEvent) {
     67       return (io.opencensus.trace.NetworkEvent) event;
     68     }
     69     io.opencensus.trace.MessageEvent messageEvent = (io.opencensus.trace.MessageEvent) event;
     70     io.opencensus.trace.NetworkEvent.Type type =
     71         (messageEvent.getType() == io.opencensus.trace.MessageEvent.Type.RECEIVED)
     72             ? io.opencensus.trace.NetworkEvent.Type.RECV
     73             : io.opencensus.trace.NetworkEvent.Type.SENT;
     74     return io.opencensus.trace.NetworkEvent.builder(type, messageEvent.getMessageId())
     75         .setUncompressedMessageSize(messageEvent.getUncompressedMessageSize())
     76         .setCompressedMessageSize(messageEvent.getCompressedMessageSize())
     77         .build();
     78   }
     79 
     80   private BaseMessageEventUtils() {}
     81 }
     82