mirror of
https://github.com/jlengrand/github-api.git
synced 2026-03-14 15:50:05 +00:00
167 lines
8.0 KiB
HTML
167 lines
8.0 KiB
HTML
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>AbstractBuilder.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">GitHub API for Java</a> > <a href="index.source.html" class="el_package">org.kohsuke.github</a> > <span class="el_source">AbstractBuilder.java</span></div><h1>AbstractBuilder.java</h1><pre class="source lang-java linenums">package org.kohsuke.github;
|
|
|
|
import java.io.IOException;
|
|
|
|
import javax.annotation.CheckForNull;
|
|
import javax.annotation.Nonnull;
|
|
|
|
/**
|
|
* An abstract data object builder/updater.
|
|
*
|
|
* This class can be use to make a Builder that supports both batch and single property changes.
|
|
* <p>
|
|
* Batching looks like this:
|
|
* </p>
|
|
*
|
|
* <pre>
|
|
* update().someName(value).otherName(value).done()
|
|
* </pre>
|
|
* <p>
|
|
* Single changes look like this:
|
|
* </p>
|
|
*
|
|
* <pre>
|
|
* set().someName(value);
|
|
* set().otherName(value);
|
|
* </pre>
|
|
* <p>
|
|
* If {@link S} is the same as {@link R}, {@link #with(String, Object)} will commit changes after the first value change
|
|
* and return a {@link R} from {@link #done()}.
|
|
* </p>
|
|
* <p>
|
|
* If {@link S} is not the same as {@link R}, {@link #with(String, Object)} will batch together multiple changes and let
|
|
* the user call {@link #done()} when they are ready.
|
|
*
|
|
* @param <R>
|
|
* Final return type built by this builder returned when {@link #done()}} is called.
|
|
* @param <S>
|
|
* Intermediate return type for this builder returned by calls to {@link #with(String, Object)}. If {@link S}
|
|
* the same as {@link R}, this builder will commit changes after each call to {@link #with(String, Object)}.
|
|
*/
|
|
abstract class AbstractBuilder<R, S> extends GitHubInteractiveObject {
|
|
|
|
@Nonnull
|
|
private final Class<R> returnType;
|
|
|
|
private final boolean commitChangesImmediately;
|
|
|
|
@CheckForNull
|
|
private final R baseInstance;
|
|
|
|
@Nonnull
|
|
protected final Requester requester;
|
|
|
|
// TODO: Not sure how update-in-place behavior should be controlled
|
|
// However, it certainly can be controlled dynamically down to the instance level or inherited for all children of
|
|
// some
|
|
// connection.
|
|
protected boolean updateInPlace;
|
|
|
|
/**
|
|
* Creates a builder.
|
|
*
|
|
* @param root
|
|
* the GitHub instance to connect to.
|
|
* @param intermediateReturnType
|
|
* the intermediate return type of type {@link S} returned by calls to {@link #with(String, Object)}.
|
|
* Must either be equal to {@code builtReturnType} or this instance must be castable to this class. If
|
|
* not, the constructor will throw {@link IllegalArgumentException}.
|
|
* @param finalReturnType
|
|
* the final return type for built by this builder returned when {@link #done()}} is called.
|
|
* @param baseInstance
|
|
* optional instance on which to base this builder.
|
|
*/
|
|
protected AbstractBuilder(@Nonnull Class<R> finalReturnType,
|
|
@Nonnull Class<S> intermediateReturnType,
|
|
@Nonnull GitHub root,
|
|
@CheckForNull R baseInstance) {
|
|
<span class="fc" id="L78"> super(root);</span>
|
|
<span class="fc" id="L79"> this.requester = root.createRequest();</span>
|
|
<span class="fc" id="L80"> this.returnType = finalReturnType;</span>
|
|
<span class="fc" id="L81"> this.commitChangesImmediately = returnType.equals(intermediateReturnType);</span>
|
|
<span class="pc bpc" id="L82" title="1 of 4 branches missed."> if (!commitChangesImmediately && !intermediateReturnType.isInstance(this)) {</span>
|
|
<span class="nc" id="L83"> throw new IllegalArgumentException(</span>
|
|
"Argument \"intermediateReturnType\": This instance must be castable to intermediateReturnType or finalReturnType must be equal to intermediateReturnType.");
|
|
}
|
|
|
|
<span class="fc" id="L87"> this.baseInstance = baseInstance;</span>
|
|
<span class="fc" id="L88"> this.updateInPlace = false;</span>
|
|
<span class="fc" id="L89"> }</span>
|
|
|
|
/**
|
|
* Finishes an update, committing changes.
|
|
*
|
|
* This method may update-in-place or not. Either way it returns the resulting instance.
|
|
*
|
|
* @return an instance with updated current data
|
|
* @throws IOException
|
|
* if there is an I/O Exception
|
|
*/
|
|
@Nonnull
|
|
@BetaApi
|
|
@Deprecated
|
|
public R done() throws IOException {
|
|
R result;
|
|
<span class="pc bpc" id="L105" title="3 of 4 branches missed."> if (updateInPlace && baseInstance != null) {</span>
|
|
<span class="nc" id="L106"> result = requester.fetchInto(baseInstance);</span>
|
|
} else {
|
|
<span class="fc" id="L108"> result = requester.fetch(returnType);</span>
|
|
}
|
|
<span class="fc" id="L110"> return result;</span>
|
|
}
|
|
|
|
/**
|
|
* Applies a value to a name for this builder.
|
|
*
|
|
* If {@link S} is the same as {@link R}, this method will commit changes after the first value change and return a
|
|
* {@link R} from {@link #done()}.
|
|
*
|
|
* If {@link S} is not the same as {@link R}, this method will return an {@link S} and letting the caller batch
|
|
* together multiple changes and call {@link #done()} when they are ready.
|
|
*
|
|
* @param name
|
|
* the name of the field
|
|
* @param value
|
|
* the value of the field
|
|
* @return either a continuing builder or an updated data record
|
|
* @throws IOException
|
|
* if an I/O error occurs
|
|
*/
|
|
@Nonnull
|
|
@BetaApi
|
|
@Deprecated
|
|
protected S with(@Nonnull String name, Object value) throws IOException {
|
|
<span class="fc" id="L134"> requester.with(name, value);</span>
|
|
<span class="fc" id="L135"> return continueOrDone();</span>
|
|
}
|
|
|
|
/**
|
|
* Chooses whether to return a continuing builder or an updated data record
|
|
*
|
|
* If {@link S} is the same as {@link R}, this method will commit changes after the first value change and return a
|
|
* {@link R} from {@link #done()}.
|
|
*
|
|
* If {@link S} is not the same as {@link R}, this method will return an {@link S} and letting the caller batch
|
|
* together multiple changes and call {@link #done()} when they are ready.
|
|
*
|
|
* @return either a continuing builder or an updated data record
|
|
* @throws IOException
|
|
* if an I/O error occurs
|
|
*/
|
|
@Nonnull
|
|
@BetaApi
|
|
@Deprecated
|
|
protected S continueOrDone() throws IOException {
|
|
// This little bit of roughness in this base class means all inheriting builders get to create Updater and
|
|
// Setter classes from almost identical code. Creator can often be implemented with significant code reuse as
|
|
// well.
|
|
<span class="fc bfc" id="L158" title="All 2 branches covered."> if (commitChangesImmediately) {</span>
|
|
// These casts look strange and risky, but they they're actually guaranteed safe due to the return path
|
|
// being based on the previous comparison of class instances passed to the constructor.
|
|
<span class="fc" id="L161"> return (S) done();</span>
|
|
} else {
|
|
<span class="fc" id="L163"> return (S) this;</span>
|
|
}
|
|
}
|
|
}
|
|
</pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.7.202105040129</span></div></body></html> |