1 /*
   2  * Copyright (c) 2009, 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 /**
  28  * Wraps the actual message or notification so that it can be
  29  * set and returned from the native receive implementation.
  30  */
  31 public class SctpResultContainer {
  32     /* static final ints so that they can be referenced from native */
  33     static final int NOTHING = 0;
  34     static final int MESSAGE = 1;
  35     static final int SEND_FAILED = 2;
  36     static final int ASSOCIATION_CHANGED = 3;
  37     static final int PEER_ADDRESS_CHANGED = 4;
  38     static final int SHUTDOWN = 5;
  39 
  40     private Object value;
  41     private int type;
  42 
  43     int type() {
  44         return type;
  45     }
  46 
  47     boolean hasSomething() {
  48         return type() != NOTHING;
  49     }
  50 
  51     boolean isNotification() {
  52         return type() != MESSAGE && type() != NOTHING ? true : false;
  53     }
  54 
  55     void clear() {
  56         type = NOTHING;
  57         value = null;
  58     }
  59 
  60     SctpNotification notification() {
  61         assert type() != MESSAGE && type() != NOTHING;
  62 
  63         return (SctpNotification) value;
  64     }
  65 
  66     SctpMessageInfoImpl getMessageInfo() {
  67         assert type() == MESSAGE;
  68 
  69         if (value instanceof SctpMessageInfoImpl)
  70             return (SctpMessageInfoImpl) value;
  71 
  72         return null;
  73     }
  74 
  75     SctpSendFailed getSendFailed() {
  76         assert type() == SEND_FAILED;
  77 
  78         if (value instanceof SctpSendFailed)
  79             return (SctpSendFailed) value;
  80 
  81         return null;
  82     }
  83 
  84     SctpAssocChange getAssociationChanged() {
  85         assert type() == ASSOCIATION_CHANGED;
  86 
  87         if (value instanceof SctpAssocChange)
  88             return (SctpAssocChange) value;
  89 
  90         return null;
  91     }
  92 
  93     SctpPeerAddrChange getPeerAddressChanged() {
  94         assert type() == PEER_ADDRESS_CHANGED;
  95 
  96         if (value instanceof SctpPeerAddrChange)
  97             return (SctpPeerAddrChange) value;
  98 
  99         return null;
 100     }
 101 
 102     SctpShutdown getShutdown() {
 103         assert type() == SHUTDOWN;
 104 
 105         if (value instanceof SctpShutdown)
 106             return (SctpShutdown) value;
 107 
 108         return null;
 109     }
 110 
 111     @Override
 112     public String toString() {
 113         StringBuilder sb = new StringBuilder();
 114         sb.append("Type: ");
 115         switch (type) {
 116             case NOTHING:              sb.append("NOTHING");             break;
 117             case MESSAGE:              sb.append("MESSAGE");             break;
 118             case SEND_FAILED:          sb.append("SEND FAILED");         break;
 119             case ASSOCIATION_CHANGED:  sb.append("ASSOCIATION CHANGE");  break;
 120             case PEER_ADDRESS_CHANGED: sb.append("PEER ADDRESS CHANGE"); break;
 121             case SHUTDOWN:             sb.append("SHUTDOWN");            break;
 122             default :                  sb.append("Unknown result type");
 123         }
 124         sb.append(", Value: ");
 125         sb.append((value == null) ? "null" : value.toString());
 126         return sb.toString();
 127     }
 128 }