1 /*
   2  * Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package sun.nio.ch.sctp;
  26 
  27 import java.lang.annotation.Native;
  28 
  29 /**
  30  * Wraps the actual message or notification so that it can be
  31  * set and returned from the native receive implementation.
  32  */
  33 public class ResultContainer {
  34     /* static final ints so that they can be referenced from native */
  35     @Native static final int NOTHING = 0;
  36     @Native static final int MESSAGE = 1;
  37     @Native static final int SEND_FAILED = 2;
  38     @Native static final int ASSOCIATION_CHANGED = 3;
  39     @Native static final int PEER_ADDRESS_CHANGED = 4;
  40     @Native static final int SHUTDOWN = 5;
  41 
  42     private Object value;
  43     private int type;
  44 
  45     int type() {
  46         return type;
  47     }
  48 
  49     boolean hasSomething() {
  50         return type() != NOTHING;
  51     }
  52 
  53     boolean isNotification() {
  54         return type() != MESSAGE && type() != NOTHING ? true : false;
  55     }
  56 
  57     void clear() {
  58         type = NOTHING;
  59         value = null;
  60     }
  61 
  62     SctpNotification notification() {
  63         assert type() != MESSAGE && type() != NOTHING;
  64 
  65         return (SctpNotification) value;
  66     }
  67 
  68     MessageInfoImpl getMessageInfo() {
  69         assert type() == MESSAGE;
  70 
  71         if (value instanceof MessageInfoImpl)
  72             return (MessageInfoImpl) value;
  73 
  74         return null;
  75     }
  76 
  77     SendFailed getSendFailed() {
  78         assert type() == SEND_FAILED;
  79 
  80         if (value instanceof SendFailed)
  81             return (SendFailed) value;
  82 
  83         return null;
  84     }
  85 
  86     AssociationChange getAssociationChanged() {
  87         assert type() == ASSOCIATION_CHANGED;
  88 
  89         if (value instanceof AssociationChange)
  90             return (AssociationChange) value;
  91 
  92         return null;
  93     }
  94 
  95     PeerAddrChange getPeerAddressChanged() {
  96         assert type() == PEER_ADDRESS_CHANGED;
  97 
  98         if (value instanceof PeerAddrChange)
  99             return (PeerAddrChange) value;
 100 
 101         return null;
 102     }
 103 
 104     Shutdown getShutdown() {
 105         assert type() == SHUTDOWN;
 106 
 107         if (value instanceof Shutdown)
 108             return (Shutdown) value;
 109 
 110         return null;
 111     }
 112 
 113     @Override
 114     public String toString() {
 115         StringBuilder sb = new StringBuilder();
 116         sb.append("Type: ");
 117         switch (type) {
 118             case NOTHING:              sb.append("NOTHING");             break;
 119             case MESSAGE:              sb.append("MESSAGE");             break;
 120             case SEND_FAILED:          sb.append("SEND FAILED");         break;
 121             case ASSOCIATION_CHANGED:  sb.append("ASSOCIATION CHANGE");  break;
 122             case PEER_ADDRESS_CHANGED: sb.append("PEER ADDRESS CHANGE"); break;
 123             case SHUTDOWN:             sb.append("SHUTDOWN");            break;
 124             default :                  sb.append("Unknown result type");
 125         }
 126         sb.append(", Value: ");
 127         sb.append((value == null) ? "null" : value.toString());
 128         return sb.toString();
 129     }
 130 }