All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.barchart.ondemand.api.CompetitorsRequest Maven / Gradle / Ivy

There is a newer version: 2.0.2
Show newest version
package com.barchart.ondemand.api;

import java.util.HashMap;
import java.util.Map;

public class CompetitorsRequest implements OnDemandRequest {

	public enum CompetitorsRequestField {
		_52_WEEK_HIGH, _52_WEEK_HIGH_DATE, _52_WEEK_LOW, _52_WEEK_LOW_DATE;

		public String getValue(CompetitorsRequestField field) {
			switch (field) {
			case _52_WEEK_HIGH:
				return "fiftyTwoWkHigh";
			case _52_WEEK_HIGH_DATE:
				return "fiftyTwoWkHighDate";
			case _52_WEEK_LOW:
				return "fiftyTwoWkLow";
			case _52_WEEK_LOW_DATE:
				return "fiftyTwoWkLowDate";
			default:
				return "";
			}
		}

		public static String forQuery(CompetitorsRequestField[] fields) {

			if (fields == null) {
				return "";
			}

			final StringBuilder sb = new StringBuilder();

			for (CompetitorsRequestField f : fields) {
				if (sb.length() > 0) {
					sb.append(',');
				}
				sb.append(f.getValue(f));
			}

			return sb.toString();

		}
	}

	private final String symbols;
	private final String fields;

	private final Map params = new HashMap();

	private CompetitorsRequest(final Builder b) {

		this.symbols = b.symbol;

		this.fields = CompetitorsRequestField.forQuery(b.fields);

		if (b.maxRecords > 0) {
			params.put("maxRecords", b.maxRecords);
		}

	}

	@Override
	public String endpoint() {

		return "getCompetitors.json";
	}

	@Override
	public String name() {

		return "Competitors";
	}

	@Override
	public Map parameters() {

		params.put("symbol", symbols);

		if (!fields.isEmpty()) {
			params.put("fields", fields);
		}

		return params;
	}

	public static class Builder {

		private String symbol;
		private CompetitorsRequestField[] fields;
		private int maxRecords;

		public Builder symbol(final String symbol) {
			this.symbol = symbol;
			return this;
		}

		public Builder eventTypes(final CompetitorsRequestField[] fields) {
			this.fields = fields;
			return this;
		}

		public Builder maxRecords(final int maxRecords) {
			this.maxRecords = maxRecords;
			return this;
		}

		public OnDemandRequest build() {
			return new CompetitorsRequest(this);
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy