/* * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package java.net.http; import java.nio.ByteBuffer; import java.util.stream.Stream; abstract class OutgoingMessage { interface Visitor { R visit(CharacterText message, A attachment); R visit(BinaryText message, A attachment); R visit(StreamedText message, A attachment); R visit(Binary message, A attachment); R visit(Ping message, A attachment); R visit(Pong message, A attachment); R visit(Close message, A attachment); } abstract R accept(Visitor visitor, A attachment); private OutgoingMessage() { } static final class CharacterText extends OutgoingMessage { public final boolean isLast; public final CharSequence characters; CharacterText(boolean isLast, CharSequence characters) { this.isLast = isLast; this.characters = characters; } @Override R accept(Visitor visitor, A attachment) { return visitor.visit(this, attachment); } @Override public String toString() { return Utils.toStringSimple(this) + "[isLast=" + isLast + ", characters=" + Utils.toString(characters) + "]"; } } static final class BinaryText extends OutgoingMessage { public final boolean isLast; public final ByteBuffer bytes; BinaryText(boolean isLast, ByteBuffer bytes) { this.isLast = isLast; this.bytes = bytes; } @Override R accept(Visitor visitor, A attachment) { return visitor.visit(this, attachment); } @Override public String toString() { return Utils.toStringSimple(this) + "[isLast=" + isLast + ", bytes=" + Utils.toString(bytes) + "]"; } } static final class StreamedText extends OutgoingMessage { public final Stream characters; StreamedText(Stream characters) { this.characters = characters; } @Override R accept(Visitor visitor, A attachment) { return visitor.visit(this, attachment); } @Override public String toString() { return Utils.toStringSimple(this) + "[characters=" + characters + "]"; } } static final class Binary extends OutgoingMessage { public final boolean isLast; public final ByteBuffer bytes; Binary(boolean isLast, ByteBuffer bytes) { this.isLast = isLast; this.bytes = bytes; } @Override R accept(Visitor visitor, A attachment) { return visitor.visit(this, attachment); } @Override public String toString() { return Utils.toStringSimple(this) + "[isLast=" + isLast + ", bytes=" + Utils.toString(bytes) + "]"; } } static final class Ping extends OutgoingMessage { public final ByteBuffer bytes; Ping(ByteBuffer bytes) { this.bytes = bytes; } @Override R accept(Visitor visitor, A attachment) { return visitor.visit(this, attachment); } @Override public String toString() { return Utils.toStringSimple(this) + "[" + Utils.toString(bytes) + "]"; } } static final class Pong extends OutgoingMessage { public final ByteBuffer bytes; Pong(ByteBuffer bytes) { this.bytes = bytes; } @Override R accept(Visitor visitor, A attachment) { return visitor.visit(this, attachment); } @Override public String toString() { return Utils.toStringSimple(this) + "[" + Utils.toString(bytes) + "]"; } } static final class Close extends OutgoingMessage { public final WebSocket.CloseCode code; public final CharSequence reason; Close() { this(null, ""); } Close(WebSocket.CloseCode code, CharSequence reason) { this.code = code; this.reason = reason; } @Override R accept(Visitor visitor, A attachment) { return visitor.visit(this, attachment); } @Override public String toString() { if (code == null) { return "Close"; } else { return Utils.toStringSimple(this) + "[code=" + code + ", reason=" + Utils.toString(reason) + "]"; } } } }