Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,89 +19,106 @@
import static com.google.common.base.Preconditions.checkNotNull;

import com.google.api.services.bigquery.model.JobReference;

import com.google.auto.value.AutoValue;
import java.io.Serializable;
import java.util.Objects;
import java.util.UUID;
import javax.annotation.Nullable;

/**
* Google BigQuery Job identity.
*/
public final class JobId implements Serializable {

private static final long serialVersionUID = 1225914835379688976L;
/** Google BigQuery Job identity. */
@AutoValue
public abstract class JobId implements Serializable {

private final String project;
private final String job;
private static final long serialVersionUID = 1225914835379688977L;

JobId() {
// Users cannot extend this, but AutoValue can.
}

/**
* Returns project's user-defined id.
* Returns job's project id.
*
* <p>When sending requests with null project, the client will attempt to infer the project name
* from the environment.
*/
public String getProject() {
return project;
}
@Nullable
public abstract String getProject();

/**
* Returns the job's id.
*
* <p>The server returns null job id for dry-run queries.
*/
@Nullable
public abstract String getJob();

/**
* Returns the job's user-defined id.
* Returns the job's location.
*
* <p>When sending requests, the location must be specified for jobs whose location not "US" or
* "EU".
*/
public String getJob() {
return job;
@Nullable
abstract String getLocation();

public abstract Builder toBuilder();

public static Builder newBuilder() {
return new AutoValue_JobId.Builder();
}

private JobId(String project, String job) {
this.project = project;
this.job = job;
@AutoValue.Builder
public abstract static class Builder {
public abstract Builder setProject(String project);

public abstract Builder setJob(String job);

/** {@code setJob} to a pseudo-random string. */
public Builder setRandomJob() {
return setJob(UUID.randomUUID().toString());
}

abstract Builder setLocation(String location);

public abstract JobId build();
}

/**
* Creates a job identity given project's and job's user-defined id.
*/
public static JobId of(String project, String job) {
return new JobId(checkNotNull(project), checkNotNull(job));
return newBuilder().setProject(checkNotNull(project)).setJob(checkNotNull(job)).build();
}

/**
* Creates a job identity given only its user-defined id.
*/
public static JobId of(String job) {
return new JobId(null, checkNotNull(job));
return newBuilder().setJob(checkNotNull(job)).build();
}

/**
* Creates a job identity with autogenerated id and no project specified.
*/
public static JobId of() {
return new JobId(null, UUID.randomUUID().toString());
}

@Override
public boolean equals(Object obj) {
return obj == this
|| obj instanceof JobId
&& Objects.equals(toPb(), ((JobId) obj).toPb());
}

@Override
public int hashCode() {
return Objects.hash(project, job);
}

@Override
public String toString() {
return toPb().toString();
return newBuilder().setRandomJob().build();
}

JobId setProjectId(String projectId) {
return getProject() != null ? this : JobId.of(projectId, getJob());
return getProject() != null ? this : toBuilder().setProject(projectId).build();
}

JobReference toPb() {
return new JobReference().setProjectId(project).setJobId(job);
return new JobReference()
.setProjectId(getProject())
.setJobId(getJob())
.setLocation(getLocation());
}

static JobId fromPb(JobReference jobRef) {
return new JobId(jobRef.getProjectId(), jobRef.getJobId());
return newBuilder()
.setProject(jobRef.getProjectId())
.setJob(jobRef.getJobId())
.setLocation(jobRef.getLocation())
.build();
}
}