Skip to content
This repository was archived by the owner on Dec 23, 2023. It is now read-only.

Commit 6bd5ee6

Browse files
authored
Merge pull request #9 from sebright/abstract-classes
Converts CensusContext, CensusContext.Builder, and CensusContextFactory into abstract classes.
2 parents d042561 + 29879bd commit 6bd5ee6

File tree

4 files changed

+25
-34
lines changed

4 files changed

+25
-34
lines changed

core/java/com/google/census/CensusContext.java

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,51 +18,58 @@
1818
/**
1919
* An immutable Census-specific context for an operation.
2020
*/
21-
public interface CensusContext {
21+
public abstract class CensusContext {
2222
/** Returns a builder based on this {@link CensusContext} */
23-
Builder builder();
23+
public abstract Builder builder();
2424

2525
/** Shorthand for builder().set(k1, v1).build() */
26-
CensusContext with(TagKey k1, TagValue v1);
26+
public final CensusContext with(TagKey k1, TagValue v1) {
27+
return builder().set(k1, v1).build();
28+
}
2729

2830
/** Shorthand for builder().set(k1, v1).set(k2, v2).build() */
29-
CensusContext with(TagKey k1, TagValue v1, TagKey k2, TagValue v2);
31+
public final CensusContext with(TagKey k1, TagValue v1, TagKey k2, TagValue v2) {
32+
return builder().set(k1, v1).set(k2, v2).build();
33+
}
3034

3135
/** Shorthand for builder().set(k1, v1).set(k2, v2).set(k3, v3).build() */
32-
CensusContext with(TagKey k1, TagValue v1, TagKey k2, TagValue v2, TagKey k3, TagValue v3);
36+
public final CensusContext with(
37+
TagKey k1, TagValue v1, TagKey k2, TagValue v2, TagKey k3, TagValue v3) {
38+
return builder().set(k1, v1).set(k2, v2).set(k3, v3).build();
39+
}
3340

3441
/**
3542
* Records the given metrics against this {@link CensusContext}.
3643
*
3744
* @param metrics the metrics to record against the saved {@link CensusContext}
3845
* @return this
3946
*/
40-
CensusContext record(MetricMap metrics);
47+
public abstract CensusContext record(MetricMap metrics);
4148

4249
/**
4350
* Serializes the {@link CensusContext} into the on-the-wire representation.
4451
*
4552
* @return serialized bytes.
4653
*/
47-
ByteBuffer serialize();
54+
public abstract ByteBuffer serialize();
4855

4956
/** Sets the current thread-local {@link CensusContext}. */
50-
void setCurrent();
57+
public abstract void setCurrent();
5158

5259
/** Builder for {@link CensusContext}. */
53-
interface Builder {
60+
public abstract static class Builder {
5461
/**
5562
* Associates the given tag key with the given tag value.
5663
*
5764
* @param key the key
5865
* @param value the value to be associated with {@code key}
5966
* @return {@code this}
6067
*/
61-
Builder set(TagKey key, TagValue value);
68+
public abstract Builder set(TagKey key, TagValue value);
6269

6370
/**
6471
* Builds a {@link CensusContext} from the specified keys and values.
6572
*/
66-
CensusContext build();
73+
public abstract CensusContext build();
6774
}
6875
}

core/java/com/google/census/CensusContextFactory.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,18 @@
2020
/**
2121
* Factory class for {@link CensusContext}.
2222
*/
23-
public interface CensusContextFactory {
23+
public abstract class CensusContextFactory {
2424
/** Creates a new {@link CensusContext} built from the given on-the-wire encoded representation.
2525
*
2626
* @param buffer on-the-wire representation of a {@link CensusContext}
2727
* @return a {@link CensusContext} deserialized from {@code buffer}
2828
*/
2929
@Nullable
30-
CensusContext deserialize(ByteBuffer buffer);
30+
public abstract CensusContext deserialize(ByteBuffer buffer);
3131

3232
/** Returns the current thread-local {@link CensusContext}. */
33-
CensusContext getCurrent();
33+
public abstract CensusContext getCurrent();
3434

3535
/** Returns the default {@link CensusContext}. */
36-
CensusContext getDefault();
36+
public abstract CensusContext getDefault();
3737
}

core_native/java/com/google/census/CensusContextFactoryImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import javax.annotation.Nullable;
2020

2121
/** Native Implementation of {@link CensusContextFactory} */
22-
class CensusContextFactoryImpl implements CensusContextFactory {
22+
final class CensusContextFactoryImpl extends CensusContextFactory {
2323
static final CensusContextImpl DEFAULT = new CensusContextImpl(new HashMap<String, String>(0));
2424

2525
/**

core_native/java/com/google/census/CensusContextImpl.java

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import java.util.HashMap;
1818

1919
/** Native Implementation of {@link CensusContext} */
20-
final class CensusContextImpl implements CensusContext {
20+
final class CensusContextImpl extends CensusContext {
2121
final HashMap<String, String> tags;
2222

2323
CensusContextImpl(HashMap<String, String> tags) {
@@ -29,22 +29,6 @@ public Builder builder() {
2929
return new Builder(tags);
3030
}
3131

32-
@Override
33-
public CensusContext with(TagKey k1, TagValue v1) {
34-
return builder().set(k1, v1).build();
35-
}
36-
37-
@Override
38-
public CensusContext with(TagKey k1, TagValue v1, TagKey k2, TagValue v2) {
39-
return builder().set(k1, v1).set(k2, v2).build();
40-
}
41-
42-
@Override
43-
public CensusContext with(
44-
TagKey k1, TagValue v1, TagKey k2, TagValue v2, TagKey k3, TagValue v3) {
45-
return builder().set(k1, v1).set(k2, v2).set(k3, v3).build();
46-
}
47-
4832
@Override
4933
public CensusContextImpl record(MetricMap stats) {
5034
return this;
@@ -75,7 +59,7 @@ public String toString() {
7559
return tags.toString();
7660
}
7761

78-
private static final class Builder implements CensusContext.Builder {
62+
private static final class Builder extends CensusContext.Builder {
7963
private final HashMap<String, String> tags;
8064

8165
private Builder(HashMap<String, String> tags) {

0 commit comments

Comments
 (0)