优化推荐

时间:2018-08-16 16:42:35

标签: java optimization

如何在以下代码中实现更多优化?,我不喜欢上次检查“ Ob​​jects.isNull”?预先感谢。

/**
 * Tries to get the service request DAO from the ItemHolder, if it is not present there, it will try to get it from the argument.
 * If no service request found then CancellationException is thrown.
 *
 * In case both are present takes precedence the one in the ItemHolder object.
 *
 * @param itemHolderParameter the holder to be checked.
 * @param serviceRequestDAO in case no service request dao is found in the holder, this will be used.
 * @return ServiceRequestDAO guaranteed not null object.
 * @throws CancellationException in case no service request DAO was retrieved.
 * @throws IllegalArgumentException in case itemHolderParameter is null.
 */
public static ServiceRequestDAO getServiceRequestDAO(final ItemHolder itemHolderParameter, final ServiceRequestDAO serviceRequestDAO){


    AtomicReference<ServiceRequestDAO> atomicReference = new AtomicReference<>(serviceRequestDAO);
    final ItemHolder itemHolder = Optional.ofNullable(itemHolderParameter).orElseThrow(() -> new IllegalArgumentException("ItemHolder must not be null"));
    Optional.ofNullable(itemHolder.getServiceRequestDAO()).ifPresent(atomicReference::set);


    //Final validation.
    final ServiceRequestDAO requestDAO = atomicReference.get();
    if(Objects.isNull(requestDAO)){
        throw new CancellationException("Unable to get ServiceRequestDAO (null)");
    }
    return requestDAO;

}

〜M

2 个答案:

答案 0 :(得分:2)

似乎没有充分的理由将directory <- "D:/sample-pdfs" pdfs <- paste(directory, "/", list.files(directory, pattern = "*.pdf"), sep = "") pdf_names <- list.files(directory, pattern = "*.pdf") pdfs_text <- map(pdfs, pdftools::pdf_text) my_data <- data_frame(document = pdf_names, text = pdfs_text) my_data %>% unnest %>% # pdfs_text is a list unnest_tokens(word, text, strip_numeric = TRUE) %>% # removing all numbers group_by(document, word) %>% summarise(count = n()) # A tibble: 4,646 x 3 # Groups: document [?] document word count <chr> <chr> <int> 1 's-Gravenhage_coalitieakkoord.pdf 1e 2 2 's-Gravenhage_coalitieakkoord.pdf 2e 2 3 's-Gravenhage_coalitieakkoord.pdf 3e 1 4 's-Gravenhage_coalitieakkoord.pdf 4e 1 5 's-Gravenhage_coalitieakkoord.pdf aan 164 6 's-Gravenhage_coalitieakkoord.pdf aanbesteding 2 7 's-Gravenhage_coalitieakkoord.pdf aanbestedingen 1 8 's-Gravenhage_coalitieakkoord.pdf aanbestedingsprocedures 1 9 's-Gravenhage_coalitieakkoord.pdf aanbevelingen 1 10 's-Gravenhage_coalitieakkoord.pdf aanbieden 4 # ... with 4,636 more rows Optional放入其中:

AtomicReference

我已尽力遵循代码的语义,但说实话,这还不清楚。

答案 1 :(得分:0)

感谢鲍里斯,

 /**
 * Tries to get the service request DAO from the ItemHolder, if it is not present there, it will try to get it from the argument.
 * If no service request found then CancellationException is thrown.
 *
 * In case both are present takes precedence the one in the ItemHolder object.
 *
 * @param itemHolderParameter the holder to be checked.
 * @param serviceRequestDAO in case no service request dao is found in the holder, this will be used.
 * @return ServiceRequestDAO guaranteed not null object.
 * @throws CancellationException in case no service request DAO was retrieved.
 * @throws IllegalArgumentException in case itemHolderParameter is null.
 */
public static ServiceRequestDAO getServiceRequestDAO(final ItemHolder itemHolderParameter, final ServiceRequestDAO serviceRequestDAO){
    return Optional.ofNullable(Optional.ofNullable(itemHolderParameter).map(ItemHolder::getServiceRequestDAO).orElse(serviceRequestDAO)).orElseThrow(() -> new CancellationException("No service request could be retrieved."));
}

测试案例:

public class HolderUtilsTest {


@Test(expected = CancellationException.class)
public void testHolderParameterNulls(){
    HolderUtils.getServiceRequestDAO(null, null);
}

@Test(expected = CancellationException.class)
public void testHolderParameterHolderNull(){
    HolderUtils.getServiceRequestDAO(new ItemHolder.Builder().build(), null);
}


@Test
public void testHolderParameterNullAndSRNotNull(){
    final ServiceRequestDAO serviceRequestDAO = Mockito.spy(new ServiceRequestDAO());
    final ServiceRequestDAO serviceRequestDAO1 = HolderUtils.getServiceRequestDAO(new ItemHolder.Builder().build(), serviceRequestDAO);
    assertThat(serviceRequestDAO1).isNotNull();
    assertThat(serviceRequestDAO1).isEqualTo(serviceRequestDAO);
}


@Test
public void testHolderParameterNotNullWithObjectAndSRNull(){
    final ServiceRequestDAO serviceRequestDAO = Mockito.spy(new ServiceRequestDAO());
    final ServiceRequestDAO serviceRequestDAO1 = HolderUtils.getServiceRequestDAO(new ItemHolder.Builder().srDao(serviceRequestDAO).build(), null);
    assertThat(serviceRequestDAO1).isNotNull();
    assertThat(serviceRequestDAO1).isEqualTo(serviceRequestDAO);

}

@Test
public void testHolderBothPresent(){

    final ServiceRequestDAO serviceRequestDAO0 = Mockito.spy(new ServiceRequestDAO());
    final ServiceRequestDAO serviceRequestDAO1 = Mockito.spy(new ServiceRequestDAO());
    final ServiceRequestDAO serviceRequestDAOResult = HolderUtils.getServiceRequestDAO(new ItemHolder.Builder().srDao(serviceRequestDAO0).build(), serviceRequestDAO1);
    assertThat(serviceRequestDAOResult).isNotNull();
    assertThat(serviceRequestDAOResult).isEqualTo(serviceRequestDAO0);
    assertThat(serviceRequestDAOResult).isNotEqualTo(serviceRequestDAO1);


}