view src/main/java/org/monetdb/mcl/net/HandshakeOption.java @ 677:e73c4a1f41c6

Add missing Copyright header info
author Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
date Thu, 03 Nov 2022 16:37:36 +0100 (2022-11-03)
parents fade6c6960cc
children aeb268156580
line wrap: on
line source
/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0.  If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * Copyright 1997 - July 2008 CWI, August 2008 - 2022 MonetDB B.V.
 */

package org.monetdb.mcl.net;

public abstract class HandshakeOption<T> {
	protected final int level;
	protected final String handshakeField;
	boolean sent = false;
	T desiredValue;

	protected HandshakeOption(int level, String handshakeField, T desiredValue) {
		if (desiredValue == null) {
			throw new IllegalArgumentException("initial value must not be null");
		}
		this.level = level;
		this.handshakeField = handshakeField;
		this.desiredValue = desiredValue;
	}

	public void set(T newValue) {
		if (newValue == null) {
			throw new IllegalArgumentException("new value must not be null");
		}
		desiredValue = newValue;
	}

	public T get() {
		return desiredValue;
	}

	public int getLevel() {
		return level;
	}

	public String getFieldName() {
		return handshakeField;
	}

	public boolean isSent() {
		return sent;
	}

	public void setSent(boolean b) {
		sent = b;
	}

	public boolean mustSend(T currentValue) {
		if (sent)
			return false;
		if (currentValue.equals(desiredValue))
			return false;
		return true;
	}

	abstract long numericValue();

	protected static class BooleanOption extends HandshakeOption<Boolean> {
		protected BooleanOption(int level, String name, Boolean initialValue) {
			super(level, name, initialValue);
		}

		@Override
		long numericValue() {
			return desiredValue ? 1 : 0;
		}
	}

	public static class AutoCommit extends BooleanOption {
		public AutoCommit(boolean autoCommit) {
			super(1, "auto_commit",  autoCommit);
		}
	}

	public static class ReplySize extends HandshakeOption<Integer> {
		public ReplySize(int size) {
			super(2, "reply_size", size);
		}

		@Override
		long numericValue() {
			return desiredValue;
		}
	}

	public static class SizeHeader extends BooleanOption {
		public SizeHeader(boolean sendHeader) {
			super(3, "size_header", sendHeader);
			set(sendHeader);
		}
	}

	public static class TimeZone extends HandshakeOption<Integer> {
		public TimeZone(int offset) {
			super(5, "time_zone", offset);
		}

		@Override
		long numericValue() {
			return desiredValue;
		}
	}
}