Files
github-api/jacoco/org.kohsuke.github/AbstractBuilder.java.html
2021-06-02 11:09:28 -07:00

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> &gt; <a href="index.source.html" class="el_package">org.kohsuke.github</a> &gt; <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.
* &lt;p&gt;
* Batching looks like this:
* &lt;/p&gt;
*
* &lt;pre&gt;
* update().someName(value).otherName(value).done()
* &lt;/pre&gt;
* &lt;p&gt;
* Single changes look like this:
* &lt;/p&gt;
*
* &lt;pre&gt;
* set().someName(value);
* set().otherName(value);
* &lt;/pre&gt;
* &lt;p&gt;
* 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()}.
* &lt;/p&gt;
* &lt;p&gt;
* 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 &lt;R&gt;
* Final return type built by this builder returned when {@link #done()}} is called.
* @param &lt;S&gt;
* 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&lt;R, S&gt; extends GitHubInteractiveObject {
@Nonnull
private final Class&lt;R&gt; 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&lt;R&gt; finalReturnType,
@Nonnull Class&lt;S&gt; 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 &amp;&amp; !intermediateReturnType.isInstance(this)) {</span>
<span class="nc" id="L83"> throw new IllegalArgumentException(</span>
&quot;Argument \&quot;intermediateReturnType\&quot;: This instance must be castable to intermediateReturnType or finalReturnType must be equal to intermediateReturnType.&quot;);
}
<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 &amp;&amp; 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>