@SuppressWarnings("unchecked") static <T extends Destination> T fromPb(String destinationPb) { if (BucketDestination.matchesDestination(destinationPb)) { return (T) BucketDestination.fromPb(destinationPb); } else if (DatasetDestination.matchesDestination(destinationPb)) { return (T) DatasetDestination.fromPb(destinationPb); } else if (TopicDestination.matchesDestination(destinationPb)) { return (T) TopicDestination.fromPb(destinationPb); } throw new IllegalArgumentException(destinationPb + " is not a valid sink destination"); } }
@Override SinkInfo parse(String... args) throws Exception { if (args.length >= 3) { if (args.length > 4) { throw new IllegalArgumentException("Too many arguments."); } String name = args[0]; Destination destination; switch (args[1]) { case "bucket": destination = BucketDestination.of(args[2]); break; case "dataset": destination = DatasetDestination.of(args[2]); break; case "topic": destination = TopicDestination.of(args[2]); break; default: throw new IllegalArgumentException("Second argument must be bucket|dataset|topic."); } SinkInfo.Builder builder = SinkInfo.newBuilder(name, destination); if (args.length == 4) { builder.setFilter(args[3]); } return builder.build(); } throw new IllegalArgumentException("Missing required sink name, destination or filter."); }
static TopicDestination fromPb(String destinationPb) { Matcher matcher = PATTERN.matcher(destinationPb); if (!matcher.matches()) { throw new IllegalArgumentException(destinationPb + " is not a valid sink destination"); } return new TopicDestination(matcher.group(1), matcher.group(2)); } }
@Override public boolean equals(Object obj) { if (obj == this) { return true; } if (obj == null || !(obj instanceof TopicDestination)) { return false; } TopicDestination other = (TopicDestination) obj; return baseEquals(other) && Objects.equals(project, other.project) && Objects.equals(topic, other.topic); }
@Override public int hashCode() { return Objects.hash(baseHashCode(), project, topic); }
/** * Creates a {@code TopicDestination} object given the name of the project and topic to be * used as sink destination. */ public static TopicDestination of(String project, String topic) { return new TopicDestination(project, topic); }
@SuppressWarnings("unchecked") static <T extends Destination> T fromPb(String destinationPb) { if (BucketDestination.matchesDestination(destinationPb)) { return (T) BucketDestination.fromPb(destinationPb); } else if (DatasetDestination.matchesDestination(destinationPb)) { return (T) DatasetDestination.fromPb(destinationPb); } else if (TopicDestination.matchesDestination(destinationPb)) { return (T) TopicDestination.fromPb(destinationPb); } throw new IllegalArgumentException(destinationPb + " is not a valid sink destination"); } }
static TopicDestination fromPb(String destinationPb) { Matcher matcher = PATTERN.matcher(destinationPb); if (!matcher.matches()) { throw new IllegalArgumentException(destinationPb + " is not a valid sink destination"); } return new TopicDestination(matcher.group(1), matcher.group(2)); } }
/** * Creates a {@code DatasetDestination} object given the name of the topic to be used as sink * destination. Topic is assumed to reside in the default project. */ public static TopicDestination of(String topic) { return new TopicDestination(null, topic); }
@Test public void testToAndFromPbDestination() { BucketDestination bucketDestination = Destination.fromPb(BUCKET_DESTINATION.toPb("other")); assertEquals(Destination.Type.BUCKET, bucketDestination.getType()); assertEquals("bucket", bucketDestination.getBucket()); compareBucketDestination(BUCKET_DESTINATION, bucketDestination); DatasetDestination datasetDestination = Destination.fromPb(DATASET_DESTINATION.toPb("other")); assertEquals(Destination.Type.DATASET, datasetDestination.getType()); assertEquals("project", datasetDestination.getProject()); assertEquals("dataset", datasetDestination.getDataset()); compareDatasetDestination(DATASET_DESTINATION, datasetDestination); TopicDestination topicDestination = Destination.fromPb(TOPIC_DESTINATION.toPb("other")); assertEquals(Destination.Type.TOPIC, topicDestination.getType()); assertEquals("project", topicDestination.getProject()); assertEquals("topic", topicDestination.getTopic()); compareTopicDestination(TOPIC_DESTINATION, topicDestination); thrown.expect(IllegalArgumentException.class); thrown.expectMessage("wrongDestination is not a valid sink destination"); Destination.fromPb("wrongDestination"); }
@Override public int hashCode() { return Objects.hash(baseHashCode(), project, topic); }
@Test public void testToAndFromPb_NoProjectId() { DatasetDestination datasetDestination = DatasetDestination.of("dataset"); SinkInfo sinkInfo = SinkInfo.of("name", DATASET_DESTINATION); compareSinkInfo( sinkInfo, SinkInfo.fromPb(SinkInfo.of("name", datasetDestination).toPb("project"))); TopicDestination topicDestination = TopicDestination.of("topic"); sinkInfo = SinkInfo.of("name", TOPIC_DESTINATION); compareSinkInfo( sinkInfo, SinkInfo.fromPb(SinkInfo.of("name", topicDestination).toPb("project"))); }
/** * Creates a {@code TopicDestination} object given the name of the project and topic to be * used as sink destination. */ public static TopicDestination of(String project, String topic) { return new TopicDestination(project, topic); }
@Test public void testToAndFromPbDestination_NoProjectId() { DatasetDestination datasetDestination = DatasetDestination.fromPb(DatasetDestination.of("dataset").toPb("project")); compareDatasetDestination(DATASET_DESTINATION, datasetDestination); assertEquals("project", datasetDestination.getProject()); TopicDestination topicDestination = TopicDestination.fromPb(TopicDestination.of("topic").toPb("project")); assertEquals("project", topicDestination.getProject()); compareTopicDestination(TOPIC_DESTINATION, topicDestination); }
@Override public boolean equals(Object obj) { if (obj == this) { return true; } if (obj == null || !(obj instanceof TopicDestination)) { return false; } TopicDestination other = (TopicDestination) obj; return baseEquals(other) && Objects.equals(project, other.project) && Objects.equals(topic, other.topic); }
private void compareTopicDestination(TopicDestination expected, TopicDestination value) { assertEquals(expected, value); assertEquals(expected.getProject(), value.getProject()); assertEquals(expected.getTopic(), value.getTopic()); assertEquals(expected.hashCode(), value.hashCode()); assertEquals(expected.toString(), value.toString()); }
/** * Creates a {@code DatasetDestination} object given the name of the topic to be used as sink * destination. Topic is assumed to reside in the default project. */ public static TopicDestination of(String topic) { return new TopicDestination(null, topic); }
@Test public void testOfTopicDestination() { assertEquals(Destination.Type.TOPIC, TOPIC_DESTINATION.getType()); assertEquals("project", TOPIC_DESTINATION.getProject()); assertEquals("topic", TOPIC_DESTINATION.getTopic()); TopicDestination topicDestination = TopicDestination.of("topic"); assertNull(topicDestination.getProject()); assertEquals("topic", topicDestination.getTopic()); }