Skip to content

Instantly share code, notes, and snippets.

@gustavopinto
Last active May 2, 2022 02:40
Show Gist options
  • Save gustavopinto/10e995bc13915c72619d4fbd55c4a8f7 to your computer and use it in GitHub Desktop.
Save gustavopinto/10e995bc13915c72619d4fbd55c4a8f7 to your computer and use it in GitHub Desktop.
Exemplos refatorações CDD
public void method() {
final EarlyAlert saved = getDao().save(earlyAlert);
sendMessageToAdvisorService.sendMessageToAdvisor(
saved, earlyAlert.getEmailCC(), LOGGER);
}
public void method() {
final EarlyAlert saved = getDao().save(earlyAlert);
try {
sendMessageToAdvisor(saved, earlyAlert.getEmailCC());
} catch (final SendFailedException e) {
LOGGER.warn("Could not send Early Alert message to advisor.", e);
throw new ValidationException("Early Alert was NOT created.", e);
}
}
public List<String> variables() {
List<String> variables = new ArrayList<>();
variables.addAll(this.queriesTemplate.getAllValues());
variables.addAll(this.headersTemplate.getAllValues());
if (this.bodyTemplate != null) {
variables.addAll(this.bodyTemplate.getVariables());
}
return variables;
}
public List<String> variables() {
List<String> variables = new ArrayList<>();
for (QueryTemplate queryTemplate : this.queries.values()) {
variables.addAll(queryTemplate.getVariables());
}
for (HeaderTemplate headerTemplate : this.headers.values()) {
variables.addAll(headerTemplate.getVariables());
}
if (this.bodyTemplate != null) {
variables.addAll(this.bodyTemplate.getVariables());
}
return variables;
}
public Map<String, Object> fillTemplateParameters(
@NotNull final EarlyAlert earlyAlert) {
EarlyAlertValidation.checkIsNull(earlyAlert);
EarlyAlertValidation.checkCreatedBy(earlyAlert);
EarlyAlertValidation.checkCampus(earlyAlert);
}
public EarlyAlert create(@NotNull final EarlyAlert earlyAlert) {
if (earlyAlert.getPerson() == null) {
throw new ValidationException("Person is missing.");
}
if (earlyAlert.getCreatedBy() == null) {
throw new ValidationException("CreeatedBy is missing.");
}
if (earlyAlert.getCampus() == null) {
throw new ValidationException("Campus is missing.");
}
}
public void method() {
for (BaseStudentReportTO person:persons) {
if (!map.containsKey(person.getSchoolId())
&& StringUtils.isNotBlank(person.getCoachSchoolId())
&& personSearchForm.getJournalSourceIds() == null
&& getDao().getJournalCountForPersonForJournalSourceIds(person.getId(), personSearchForm.getJournalSourceIds()) != 0) {
final JournalCaseNotesStudentReportTO entry = new JournalCaseNotesStudentReportTO(person);
personsWithJournalEntries.add(entry);
map.put(entry.getSchoolId(), entry);
}
}
}
public void method() {
for (BaseStudentReportTO person:persons) {
if (!map.containsKey(person.getSchoolId()) && StringUtils.isNotBlank(person.getCoachSchoolId())) {
boolean addStudent = true;
if (personSearchForm.getJournalSourceIds()!=null) {
if (getDao().getJournalCountForPersonForJournalSourceIds(person.getId(), personSearchForm.getJournalSourceIds()) == 0) {
addStudent = false;
}
}
if (addStudent) {
final JournalCaseNotesStudentReportTO entry = new JournalCaseNotesStudentReportTO(person);
personsWithJournalEntries.add(entry);
map.put(entry.getSchoolId(), entry);
}
}
}
}
public void method() {
for(UUID coachId: easByCoach.keySet()){
Map<String,Object> messageParams = new HashMap<>();
easByCoach.get(coachId).sort(MessageParamsComparator::compareParams);
}
}
public class MessageParamsComparator {
public int compare(EarlyAlertTO p1, EarlyAlertTO p2) {
Date p1Date = p1.getLastResponseDate();
if (p1Date == null)
p1Date = p1.getCreatedDate();
Date p2Date = p2.getLastResponseDate();
if (p2Date == null)
p2Date = p2.getCreatedDate();
return p1Date.compareTo(p2Date);
}
}
public void method() {
for(UUID coachId: easByCoach.keySet()){
Map<String,Object> messageParams = new HashMap<String,Object>();
Collections.sort(easByCoach.get(coachId), new Comparator<EarlyAlertTO>() {
@Override
public int compare(EarlyAlertTO p1, EarlyAlertTO p2) {
Date p1Date = p1.getLastResponseDate();
if (p1Date == null)
p1Date = p1.getCreatedDate();
Date p2Date = p2.getLastResponseDate();
if (p2Date == null)
p2Date = p2.getCreatedDate();
return p1Date.compareTo(p2Date);
}
});
}
}
import java.io.Serializable;
import java.net.URI;
import java.nio.charset.Charset;
import feign.Request.HttpMethod;
import feign.template.BodyTemplate;
import feign.template.HeaderTemplate;
import feign.template.TemplateChunk;
import feign.template.UriTemplate;
import feign.template.UriUtils;
import static feign.Util.CONTENT_LENGTH;
import static feign.Util.checkNotNull;
import java.io.Serializable;
import java.net.URI;
import java.nio.charset.Charset;
import feign.Request.HttpMethod;
import feign.template.*;
import static feign.Util.*;
public static RequestTemplate from(RequestTemplate requestTemplate) {
RequestTemplate template = new RequestTemplate(...);
template.queriesTemplate.putIfEmpty(requestTemplate.queriesTemplate);
template.headersTemplate.putIfEmpty(requestTemplate.headersTemplate);
return template;
}
public static RequestTemplate from(RequestTemplate requestTemplate) {
RequestTemplate template = new RequestTemplate(...);
if (!requestTemplate.queries().isEmpty()) {
template.queries.putAll(requestTemplate.queries);
}
if (!requestTemplate.headers().isEmpty()) {
template.headers.putAll(requestTemplate.headers);
}
return template;
}
public Request request() {
Asserts.booleanStateMustBeTrue(this.resolved, "template has not been resolved.");
return Request.create(method, url(), headers(), body, this);
}
public Request request() {
if (!this.resolved) {
throw new IllegalStateException("template has not been resolved.");
}
return Request.create(method, url(), headers(), body, this);
}
public String method() {
return Optional.ofNullable(method).map(HttpMethod::name).orElse(null);
}
public String method() {
return (method != null) ? method.name() : null;
}
public String url() {
return PathBuilder.withAllQueryParameters(this);
}
public class PathBuilder {
public static String withAllQueryParameters (RequestTemplate requestTemplate) { ... }
}
public String url() {
StringBuilder url = new StringBuilder(this.path());
if (!this.queries.isEmpty()) {
url.append(this.queryLine());
}
if (fragment != null) {
url.append(fragment);
}
return url.toString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment