jdk/src/java.httpclient/share/classes/java/net/http/WSOutgoingMessage.java
changeset 37874 02589df0999a
child 39133 b5641ce64cf7
equal deleted inserted replaced
37858:7c04fcb12bd4 37874:02589df0999a
       
     1 /*
       
     2  * Copyright (c) 2016, 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  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  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  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 java.net.http;
       
    26 
       
    27 import java.nio.ByteBuffer;
       
    28 import java.util.stream.Stream;
       
    29 
       
    30 abstract class WSOutgoingMessage {
       
    31 
       
    32     interface Visitor {
       
    33         void visit(Text message);
       
    34         void visit(StreamedText message);
       
    35         void visit(Binary message);
       
    36         void visit(Ping message);
       
    37         void visit(Pong message);
       
    38         void visit(Close message);
       
    39     }
       
    40 
       
    41     abstract void accept(Visitor visitor);
       
    42 
       
    43     private WSOutgoingMessage() { }
       
    44 
       
    45     static final class Text extends WSOutgoingMessage {
       
    46 
       
    47         public final boolean isLast;
       
    48         public final CharSequence characters;
       
    49 
       
    50         Text(boolean isLast, CharSequence characters) {
       
    51             this.isLast = isLast;
       
    52             this.characters = characters;
       
    53         }
       
    54 
       
    55         @Override
       
    56         void accept(Visitor visitor) {
       
    57             visitor.visit(this);
       
    58         }
       
    59 
       
    60         @Override
       
    61         public String toString() {
       
    62             return WSUtils.toStringSimple(this) + "[isLast=" + isLast
       
    63                     + ", characters=" + WSUtils.toString(characters) + "]";
       
    64         }
       
    65     }
       
    66 
       
    67     static final class StreamedText extends WSOutgoingMessage {
       
    68 
       
    69         public final Stream<? extends CharSequence> characters;
       
    70 
       
    71         StreamedText(Stream<? extends CharSequence> characters) {
       
    72             this.characters = characters;
       
    73         }
       
    74 
       
    75         @Override
       
    76         void accept(Visitor visitor) {
       
    77             visitor.visit(this);
       
    78         }
       
    79 
       
    80         @Override
       
    81         public String toString() {
       
    82             return WSUtils.toStringSimple(this) + "[characters=" + characters + "]";
       
    83         }
       
    84     }
       
    85 
       
    86     static final class Binary extends WSOutgoingMessage {
       
    87 
       
    88         public final boolean isLast;
       
    89         public final ByteBuffer bytes;
       
    90 
       
    91         Binary(boolean isLast, ByteBuffer bytes) {
       
    92             this.isLast = isLast;
       
    93             this.bytes = bytes;
       
    94         }
       
    95 
       
    96         @Override
       
    97         void accept(Visitor visitor) {
       
    98             visitor.visit(this);
       
    99         }
       
   100 
       
   101         @Override
       
   102         public String toString() {
       
   103             return WSUtils.toStringSimple(this) + "[isLast=" + isLast
       
   104                     + ", bytes=" + WSUtils.toString(bytes) + "]";
       
   105         }
       
   106     }
       
   107 
       
   108     static final class Ping extends WSOutgoingMessage {
       
   109 
       
   110         public final ByteBuffer bytes;
       
   111 
       
   112         Ping(ByteBuffer bytes) {
       
   113             this.bytes = bytes;
       
   114         }
       
   115 
       
   116         @Override
       
   117         void accept(Visitor visitor) {
       
   118             visitor.visit(this);
       
   119         }
       
   120 
       
   121         @Override
       
   122         public String toString() {
       
   123             return WSUtils.toStringSimple(this) + "[" + WSUtils.toString(bytes) + "]";
       
   124         }
       
   125     }
       
   126 
       
   127     static final class Pong extends WSOutgoingMessage {
       
   128 
       
   129         public final ByteBuffer bytes;
       
   130 
       
   131         Pong(ByteBuffer bytes) {
       
   132             this.bytes = bytes;
       
   133         }
       
   134 
       
   135         @Override
       
   136         void accept(Visitor visitor) {
       
   137             visitor.visit(this);
       
   138         }
       
   139 
       
   140         @Override
       
   141         public String toString() {
       
   142             return WSUtils.toStringSimple(this) + "[" + WSUtils.toString(bytes) + "]";
       
   143         }
       
   144     }
       
   145 
       
   146     static final class Close extends WSOutgoingMessage {
       
   147 
       
   148         public final ByteBuffer bytes;
       
   149 
       
   150         Close(ByteBuffer bytes) {
       
   151             this.bytes = bytes;
       
   152         }
       
   153 
       
   154         @Override
       
   155         void accept(Visitor visitor) {
       
   156             visitor.visit(this);
       
   157         }
       
   158 
       
   159         @Override
       
   160         public String toString() {
       
   161             return WSUtils.toStringSimple(this) + "[" + WSUtils.toString(bytes) + "]";
       
   162         }
       
   163     }
       
   164 }