Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Licensed to the Apache Software Foundation (ASF) under one or more
0003  * contributor license agreements.  See the NOTICE file distributed with
0004  * this work for additional information regarding copyright ownership.
0005  * The ASF licenses this file to You under the Apache License, Version 2.0
0006  * (the "License"); you may not use this file except in compliance with
0007  * the License.  You may obtain a copy of the License at
0008  *
0009  *    http://www.apache.org/licenses/LICENSE-2.0
0010  *
0011  * Unless required by applicable law or agreed to in writing, software
0012  * distributed under the License is distributed on an "AS IS" BASIS,
0013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014  * See the License for the specific language governing permissions and
0015  * limitations under the License.
0016  */
0017 
0018 package org.apache.spark.network.protocol;
0019 
0020 import java.util.Objects;
0021 
0022 import io.netty.buffer.ByteBuf;
0023 import org.apache.commons.lang3.builder.ToStringBuilder;
0024 import org.apache.commons.lang3.builder.ToStringStyle;
0025 
0026 /**
0027 * Encapsulates a request for a particular chunk of a stream.
0028 */
0029 public final class StreamChunkId implements Encodable {
0030   public final long streamId;
0031   public final int chunkIndex;
0032 
0033   public StreamChunkId(long streamId, int chunkIndex) {
0034     this.streamId = streamId;
0035     this.chunkIndex = chunkIndex;
0036   }
0037 
0038   @Override
0039   public int encodedLength() {
0040     return 8 + 4;
0041   }
0042 
0043   public void encode(ByteBuf buffer) {
0044     buffer.writeLong(streamId);
0045     buffer.writeInt(chunkIndex);
0046   }
0047 
0048   public static StreamChunkId decode(ByteBuf buffer) {
0049     assert buffer.readableBytes() >= 8 + 4;
0050     long streamId = buffer.readLong();
0051     int chunkIndex = buffer.readInt();
0052     return new StreamChunkId(streamId, chunkIndex);
0053   }
0054 
0055   @Override
0056   public int hashCode() {
0057     return Objects.hash(streamId, chunkIndex);
0058   }
0059 
0060   @Override
0061   public boolean equals(Object other) {
0062     if (other instanceof StreamChunkId) {
0063       StreamChunkId o = (StreamChunkId) other;
0064       return streamId == o.streamId && chunkIndex == o.chunkIndex;
0065     }
0066     return false;
0067   }
0068 
0069   @Override
0070   public String toString() {
0071     return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
0072       .append("streamId", streamId)
0073       .append("chunkIndex", chunkIndex)
0074       .toString();
0075   }
0076 }