Buggy code:
@ Value ( "${architecture.default.code}" ) private String defaultArchitectureCode ; private ImageHelper imageHelper = new ImageHelper ( ) ; private static byte [ ] toByteArray ( InputStream inputStream , int sizeLimit ) throws IOException { } public Optional < PkgVersion > getLatestPkgVersionForPkgName ( ObjectContext context , String pkgName ) { } public Optional < PkgVersion > getLatestPkgVersionForPkg ( ObjectContext context , Pkg pkg ) { } public Optional < PkgVersion > getLatestPkgVersionForPkg ( ObjectContext context , Pkg pkg , final List < Architecture > architectures ) { } public List < PkgVersion > search ( ObjectContext context , PkgSearchSpecification search ) { } public long total ( ObjectContext context , PkgSearchSpecification search ) { } private void appendEjbqlAllPkgsWhere ( Appendable ejbql , List < Object > parameterList , ObjectContext context , boolean allowSourceOnly ) throws IOException { } public long totalPkg ( ObjectContext context , boolean allowSourceOnly ) { } public long eachPkg ( ObjectContext context , boolean allowSourceOnly , Callback < Pkg > c ) { } public PkgIcon storePkgIconImage ( InputStream input , MediaType mediaType , Integer expectedSize , ObjectContext context , Pkg pkg ) throws IOException , BadPkgIconException { } private List < MediaType > getInUsePkgIconMediaTypes ( final ObjectContext context ) { } private List < Integer > getInUsePkgIconSizes ( ObjectContext context , MediaType mediaType ) { } public List < PkgIconConfiguration > getInUsePkgIconConfigurations ( ObjectContext objectContext ) { } public void writePkgScreenshotImage ( OutputStream output , ObjectContext context , PkgScreenshot screenshot , int targetWidth , int targetHeight ) throws IOException { } public PkgScreenshot storePkgScreenshotImage ( InputStream input , ObjectContext context , Pkg pkg ) throws IOException , BadPkgScreenshotException { } public long payloadLength ( PkgVersion pkgVersion ) throws IOException { } public int deactivatePkgVersionsForPkgAssociatedWithRepository ( ObjectContext context , Pkg pkg , final Repository repository ) { } public Set < String > fetchPkgNamesWithAnyPkgVersionAssociatedWithRepository ( ObjectContext context , Repository repository ) { } private VersionCoordinates toVersionCoordinates ( org . haikuos . pkg . model . PkgVersion version ) { } public void importFrom ( ObjectContext objectContext , ObjectId repositoryObjectId , org . haikuos . pkg . model . Pkg pkg , boolean populatePayloadLength ) { Preconditions . checkNotNull ( pkg ) ; Preconditions . checkNotNull ( repositoryObjectId ) ; Repository repository = Repository . get ( objectContext , repositoryObjectId ) ; Optional < org . haikuos . haikudepotserver . dataobjects . Pkg > persistedPkgOptional = org . haikuos . haikudepotserver . dataobjects . Pkg . getByName ( objectContext , pkg . getName ( ) ) ; org . haikuos . haikudepotserver . dataobjects . Pkg persistedPkg ; Optional < org . haikuos . haikudepotserver . dataobjects . PkgVersion > persistedLatestExistingPkgVersion = Optional . empty ( ) ; Architecture architecture = Architecture . getByCode ( objectContext , pkg . getArchitecture ( ) . name ( ) . toLowerCase ( ) ) . get ( ) ; org . haikuos . haikudepotserver . dataobjects . PkgVersion persistedPkgVersion = null ; if ( ! ( persistedPkgOptional . isPresent ( ) ) ) { persistedPkg = objectContext . newObject ( org . haikuos . haikudepotserver . dataobjects . Pkg . class ) ; persistedPkg . setName ( pkg . getName ( ) ) ; persistedPkg . setActive ( Boolean . TRUE ) ; persistedPkg . setProminence ( Prominence . getByOrdering ( objectContext , ORDERING_LAST ) . get ( ) ) ; PkgOrchestrationService . LOGGER . info ( "the<seq2seq4repair_space>package<seq2seq4repair_space>{}<seq2seq4repair_space>did<seq2seq4repair_space>not<seq2seq4repair_space>exist;<seq2seq4repair_space>will<seq2seq4repair_space>create" , pkg . getName ( ) ) ; } else { persistedPkg = persistedPkgOptional . get ( ) ; List < Expression > expressions = ImmutableList . of ( ExpressionFactory . matchExp ( org . haikuos . haikudepotserver . dataobjects . PkgVersion , persistedPkg ) , ExpressionHelper . toExpression ( toVersionCoordinates ( pkg . getVersion ( ) ) ) , ExpressionFactory . matchExp ( ARCHITECTURE_PROPERTY , architecture ) ) ; SelectQuery selectQuery = new SelectQuery ( org . haikuos . haikudepotserver . dataobjects . PkgVersion . class , ExpressionHelper . andAll ( expressions ) ) ; <START_BUG> persistedPkgVersion = ( ( List < org . haikuos . haikudepotserver . dataobjects . PkgVersion > ) ( objectContext . performQuery ( selectQuery ) ) ) . stream ( ) . collect ( SingleCollector . single ( ) ) ; <END_BUG>

Fix:
persistedPkgVersion = ( ( List < org . haikuos . haikudepotserver . dataobjects . PkgVersion > ) ( objectContext . performQuery ( selectQuery ) ) ) . stream ( ) . collect ( SingleCollector . optional ( ) ) . orElse ( null ) ;