Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (c) 2008 The Board of Trustees of The Leland Stanford Junior |
| 3 | * University |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 6 | * not use this file except in compliance with the License. You may obtain |
| 7 | * a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | * License for the specific language governing permissions and limitations |
| 15 | * under the License. |
| 16 | **/ |
| 17 | |
| 18 | package org.openflow.types; |
| 19 | |
Andreas Wundsam | f22c2e2 | 2013-08-02 22:21:20 -0700 | [diff] [blame] | 20 | import org.jboss.netty.buffer.ChannelBuffer; |
| 21 | import org.openflow.exceptions.OFParseError; |
| 22 | import org.openflow.protocol.OFMessageReader; |
| 23 | import org.openflow.protocol.Writeable; |
| 24 | |
| 25 | public class U32 implements Writeable { |
| 26 | private final int raw; |
| 27 | |
| 28 | private U32(int raw) { |
| 29 | this.raw = raw; |
| 30 | } |
| 31 | |
| 32 | public static U32 of(long value) { |
| 33 | return new U32(U32.t(value)); |
| 34 | } |
| 35 | |
| 36 | public static U32 ofRaw(int value) { |
| 37 | return new U32(value); |
| 38 | } |
| 39 | |
Yotam Harchol | 5804f77 | 2013-08-21 17:35:31 -0700 | [diff] [blame^] | 40 | public long getValue() { |
Andreas Wundsam | f22c2e2 | 2013-08-02 22:21:20 -0700 | [diff] [blame] | 41 | return f(raw); |
| 42 | } |
| 43 | |
Yotam Harchol | 5804f77 | 2013-08-21 17:35:31 -0700 | [diff] [blame^] | 44 | public int getRaw() { |
Andreas Wundsam | f22c2e2 | 2013-08-02 22:21:20 -0700 | [diff] [blame] | 45 | return raw; |
| 46 | } |
| 47 | |
| 48 | @Override |
| 49 | public String toString() { |
| 50 | return "" + f(raw); |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | public int hashCode() { |
| 55 | final int prime = 31; |
| 56 | int result = 1; |
| 57 | result = prime * result + raw; |
| 58 | return result; |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public boolean equals(Object obj) { |
| 63 | if (this == obj) |
| 64 | return true; |
| 65 | if (obj == null) |
| 66 | return false; |
| 67 | if (getClass() != obj.getClass()) |
| 68 | return false; |
| 69 | U32 other = (U32) obj; |
| 70 | if (raw != other.raw) |
| 71 | return false; |
| 72 | return true; |
| 73 | } |
| 74 | |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 75 | public static long f(final int i) { |
| 76 | return i & 0xffffffffL; |
| 77 | } |
| 78 | |
| 79 | public static int t(final long l) { |
| 80 | return (int) l; |
| 81 | } |
Andreas Wundsam | f22c2e2 | 2013-08-02 22:21:20 -0700 | [diff] [blame] | 82 | |
| 83 | @Override |
| 84 | public void writeTo(ChannelBuffer bb) { |
| 85 | bb.writeInt(raw); |
| 86 | } |
| 87 | |
| 88 | public final static Reader READER = new Reader(); |
| 89 | |
| 90 | private static class Reader implements OFMessageReader<U32> { |
| 91 | @Override |
| 92 | public U32 readFrom(ChannelBuffer bb) throws OFParseError { |
| 93 | return new U32(bb.readInt()); |
| 94 | } |
| 95 | } |
| 96 | |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 97 | } |